Iphone 在UIImage中获取细分图像的最佳方法是什么?
例如:如果我有一个男人的形象, 我想从人体的不同部位(头部、腹部)触发这个事件Iphone 在UIImage中获取细分图像的最佳方法是什么?,iphone,objective-c,Iphone,Objective C,例如:如果我有一个男人的形象, 我想从人体的不同部位(头部、腹部)触发这个事件 那么,制作该图像并从UIImageView按语法触发的最佳方法是什么呢?我会找出图像中要触发不同事件的区域,然后使用UIGestureRecognizer或UIView的“触碰”方法的覆盖来确定触碰是否落在其中一个区域内。然后,您需要确定触摸的位置是否在使用2D形状点测试()定义的区域内。根据您定义的区域的复杂性,此测试可能会更复杂。我通过此测试得到了解决方案: 我有大小相同的不同区域的切片图像, 然后像一张图片一样
那么,制作该图像并从UIImageView按语法触发的最佳方法是什么呢?我会找出图像中要触发不同事件的区域,然后使用UIGestureRecognizer或UIView的“触碰”方法的覆盖来确定触碰是否落在其中一个区域内。然后,您需要确定触摸的位置是否在使用2D形状点测试()定义的区域内。根据您定义的区域的复杂性,此测试可能会更复杂。我通过此测试得到了解决方案: 我有大小相同的不同区域的切片图像, 然后像一张图片一样排列。 现在点击我刚刚检查了一下图片点击完成 这是那个的代码
-(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;
}
嘿,brijesh,我有一张地图,想显示我点击的区域名称,那么我可以使用这种方法吗?当然,为此,你必须创建不同区域的图像,所有区域的大小都相同,除了图像中的区域部分将是透明的。然后,您必须将上述代码与map一起使用。