2012-04-09 18 views
0

在我看來,我有幾個子視圖。這是UIImageView。驗證具有座標的像素(iOS)

每個UIImageView都包含帶有alpha通道的圖像。

這是一個圖像: enter image description here

我使用下面,用於檢測我在位置視圖觸摸此方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 

    NSArray *views = [self.view subviews]; 

    for (UIView *v in views) { 
     if([v isKindOfClass:[Piece class]]){ 
      if (CGRectContainsPoint(v.frame, touchLocation) && ((Piece *)v).snapped == FALSE) { 

       UITouch* touchInPiece = [touches anyObject]; 
       CGPoint point = [touchInPiece locationInView:(Piece *)v]; 
       BOOL solidColor = [self verifyAlphaPixelImage:(Piece *)v atX:point.x atY:point.y]; 

       if (solidColor) {      
        dragging = YES; 
        oldX = touchLocation.x; 
        oldY = touchLocation.y; 
        piece = (Piece *)v; 
        [self.view bringSubviewToFront:piece]; 
        break; 
       }    

      } 
     } 
    } 
} 

,並且此方法對於驗證阿爾法像素

- (BOOL)verifyAlphaPixelImage:(Piece *)image atX:(int)x atY:(int)y{ 

    CGImageRef imageRef = [image.image CGImage]; 
    NSUInteger width = CGImageGetWidth(imageRef); 
    NSUInteger height = CGImageGetHeight(imageRef); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    unsigned char *rawData = malloc(height * width * 4); 
    NSUInteger bytesPerPixel = 4; 
    NSUInteger bytesPerRow = bytesPerPixel * width; 
    NSUInteger bitsPerComponent = 8; 
    CGContextRef context = CGBitmapContextCreate(rawData, width, height, 
               bitsPerComponent, bytesPerRow, colorSpace, 
               kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
    CGColorSpaceRelease(colorSpace);  
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
    CGContextRelease(context);  
    // Now your rawData contains the image data in the RGBA8888 pixel format. 
    int byteIndex = (bytesPerRow * y) + x * bytesPerPixel; 
    // CGFloat red = (rawData[byteIndex] ) ; 
    // CGFloat green = (rawData[byteIndex + 1]) ; 
    // CGFloat blue = (rawData[byteIndex + 2]) ; 
    CGFloat alpha = (rawData[byteIndex + 3]) ; 
    NSLog(@"%f", alpha); 
    free(rawData);  
    if(alpha==255.0) return NO; 
    else return YES; 



} 

如果創建alpha像素我需要觸摸UIImageView下面的其他UIImageView,我之前已經使用了tapp。

例如,如果我已經堆疊的UIImageView和我觸及第一:

現在我應該驗證第一的UIImageView

如果我觸碰上阿爾法像素 - >我應該移動到下一個的UIImageView與該座標和驗證它也適用於alpha像素。

如果第三,第四或第五沒有與我的座標alpha像素,我應該選擇這個UIImageView。

現在我驗證我的像素 - 但我的方法返回錯誤的值。

回答

1
  1. 在關於How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?的討論中,您正在使用的 例程存在更正。使用calloc而不是malloc,否則你會開始 獲得任意結果。
  2. 如果您的Piece課程對圖像進行縮放,則需要按照圖像顯示的比例縮放它的x和y輸入。
  3. touchesBegan方法奇怪地看着它包含的其他視圖,而不是它本身。是否有一個原因? touchesBegan是什麼課程?
  4. 子視圖按照從後到前的繪製順序存儲,所以當您遍歷子視圖時,您會首先查看(並可能選擇)最後面的對象。而不是從subviews的最後一個元素迭代到前面。
  5. 即使這樣做後,它將是非常低效的,每次用戶點擊時呈現每片圖像。您最終需要緩存每個Piece的像素數據以進行快速查找。
+0

謝謝,你的回答真的幫了我很大的忙! – 2012-04-16 22:54:21