2012-04-09 59 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