在UIImage中获取故障图像的最佳方法是什么?

时间:2011-03-17 11:37:28

标签: iphone objective-c

例如:如果我有一个人的形象, 我想从人体的不同部位(从头部,胃部)触发事件。

那么制作该图像并从UIImageView以编程方式触发的最佳方法是什么

2 个答案:

答案 0 :(得分:1)

我会找出你想要触发不同事件的图像区域,然后使用 UIGestureRecoginzer 或覆盖 UIView 的“touches”方法确定触摸是否属于这些区域之一。然后,您需要使用2D点形测试(here's a really good example of this)确定触摸的位置是否在您定义的区域内。根据您定义的区域的复杂程度,此测试可能会更复杂。

答案 1 :(得分:1)

我得到了这个解决方案:

我有相同大小的不同区域的切片图像, 并安排它像一个图像。 现在点击我只需检查图像上点击完成....

这是代码...

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

UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];

BOOL alphaFace = [self isPointTransparent:point addImage:[UIImage imageNamed:@"face.png"]];
BOOL alphaHand = [self isPointTransparent:point addImage:[UIImage imageNamed:@"hand.png"]];
BOOL alphaMidStomach = [self isPointTransparent:point addImage:[UIImage imageNamed:@"midStomach.png"]];
NSString *filePath=[[NSBundle mainBundle] pathForResource:@"beep" ofType:@"wav"];;
NSURL *fileURL= [[NSURL alloc] initFileURLWithPath:filePath];;

    if(alphaFace)
    {
        // Clicked on Face Image
    }
    else if(alphaHand)
    {
        // Clicked on Hand Image
    }
    else if(alphaMidStomach)
    {
        // Clicked on Stomach Image
    }
    // Now just check alplha , if it's No then do something....

 }   
  -CGContextRef CreateARGBBitmapContext (CGImageRef inImage)
  {
CGContextRef    context = NULL;
CGColorSpaceRef colorSpace = NULL; // tell we want  kCGImageAlphaOnly
void *          bitmapData;
int             bitmapByteCount;
int             bitmapBytesPerRow;


size_t pixelsWide = CGImageGetWidth(inImage);
size_t pixelsHigh = CGImageGetHeight(inImage);
bitmapBytesPerRow   = (pixelsWide * 1); // 8bpp
bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

bitmapData = calloc(1, bitmapByteCount );
if (bitmapData == NULL) 
{
    CGColorSpaceRelease( colorSpace );
    return nil;
}
context = CGBitmapContextCreate (bitmapData,
                                 pixelsWide,
                                 pixelsHigh,
                                 8,
                                 bitmapBytesPerRow,
                                 colorSpace,
                                 kCGImageAlphaOnly);
if (context == NULL)
{
    free (bitmapData);
    fprintf (stderr, "Context not created!");
}
CGColorSpaceRelease( colorSpace );

return context;
  }

 - (NSData *) ARGBData:(UIImage*)image  {
CGContextRef cgctx = CreateARGBBitmapContext(image.CGImage);
if (cgctx == NULL) 
    return nil;

size_t w = CGImageGetWidth(image.CGImage);
size_t h = CGImageGetHeight(image.CGImage);
CGRect rect = {{0,0},{w,h}}; 
CGContextDrawImage(cgctx, rect, image.CGImage); 

unsigned char *data = CGBitmapContextGetData (cgctx);
CGContextRelease(cgctx); 
if (!data)
    return nil;

size_t dataSize = 1 * w * h; // 8 bits

return [NSData dataWithBytes:data length:dataSize];
 }

 -(BOOL) isPointTransparent: (CGPoint) point addImage:(UIImage*)image {
NSData *rawData = [self ARGBData:image];  // Or cache this
if (rawData == nil)
    return NO;

// just 8 bits per alpha component
size_t bpp = 1;
size_t bpr = 320 * 1;

NSUInteger index = (point.x * bpp) + (point.y * bpr);
unsigned char *rawDataBytes = (unsigned char *)[rawData bytes];

return rawDataBytes[index] == 0;    
}