【发布时间】:2013-10-31 09:31:39
【问题描述】:
我有一个 .PNG 文件。该图像具有白色背景,只有黑色线条。
我想在用户触摸图像时检测到黑线。在这张图片中,我如何通过触摸检测出花朵的线条
例如
这怎么可能?
请帮帮我……
【问题讨论】:
-
我的问题有什么问题?为什么要投反对票?
标签: ios ipad cocoa-touch core-graphics
我有一个 .PNG 文件。该图像具有白色背景,只有黑色线条。
我想在用户触摸图像时检测到黑线。在这张图片中,我如何通过触摸检测出花朵的线条
例如
这怎么可能?
请帮帮我……
【问题讨论】:
标签: ios ipad cocoa-touch core-graphics
好的解决方案只有这个类会帮助UIView+ColorOfPoint
【讨论】:
创建UIView类的类别。添加以下函数。
@implementation UIView (ColorOfPoint)
- (UIColor *) colorOfPoint:(CGPoint)point
{
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapFloatComponents);
CGContextTranslateCTM(context, -point.x, -point.y);
[self.layer renderInContext:context];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];
return color;
}
@end
通过以下调用使用它:
UIColor *col = [YOUR_IMAGE_VIEW colorOfPoint:CGPOINT_WHERE_USER_TOUCHED];
【讨论】: