我的应用程序中有一个UIImageView,并且我已将手势识别器附加到该图像视图。
我的问题是,只有在触摸其可见区域时才需要调用它的操作,而不是在其透明部分发生触摸时。
我怎样才能做到这一点?
我的应用程序中有一个UIImageView,并且我已将手势识别器附加到该图像视图。
我的问题是,只有在触摸其可见区域时才需要调用它的操作,而不是在其透明部分发生触摸时。
我怎样才能做到这一点?
这些代码片段应该可以帮助您入门。第一篇文章是一个类别UIView(我在 SO 上的某个地方,但我不记得在哪里)。This gets you the color under the touch point.
@interface UIView (ColorOfPoint)
- (UIColor *) colorOfPoint:(CGPoint)point;
@end
- (UIColor *) colorOfPoint:(CGPoint)point
{
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);
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;
}
第二部分展示了我如何在自定义图像视图类中调用此方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint loc = [touch locationInView:self];
self.pickedColor = [self colorOfPoint:loc];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ColorPicked" object:self userInfo:nil];
}
我在这里使用了一个通知,因为我需要让另一个类知道所选择颜色的值。
您可以使用它来获取接触点的 Alpha
-(UIColor*) getPixelColorAtLocation:(CGPoint)point
{
CGImageRef inImage;
UIColor *color = nil;
inImage = [self.imageView.image CGImage];
CGContextRef context = [self createARGBBitmapContextFromImage:inImage];
如果(上下文 == NULL)返回零;
size_t w = CGImageGetWidth(inImage);
size_th = CGImageGetHeight(inImage);
CGRect 矩形 = {{0,0},{w,h}};
// 将图像绘制到位图上下文。一旦我们画画,记忆
// 分配给渲染的上下文将包含
// 指定颜色空间中的原始图像数据。
CGContextDrawImage(context, rect, inImage);
// 现在我们可以获得一个指向与位图关联的图像数据的指针
// 语境。
无符号字符 * 数据 = CGBitmapContextGetData(上下文);
如果(数据!= NULL)
{
//offset 从 x,y 定位数据中的像素。
//4代表每像素4字节数据,w为一行数据的宽度。
整数偏移量;
偏移量 = 4*((w*round(point.y))+round(point.x));
int alpha = 数据[偏移量];
诠释红色=数据[偏移+1];
诠释绿色=数据[偏移+2];
诠释蓝色=数据[偏移+3];
//NSLog(@"%@",name);
NSLog(@"offset: %i 颜色: RGB A %i %i %i %i ",offset,red,green,blue,alpha);
color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
// 完成后,释放上下文
CGContextRelease(上下文);
// 为上下文释放图像数据内存
如果(数据)
{
免费(数据);
}
返回颜色;
}
-(CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage
{
CGContextRef 上下文 = NULL;
CGColorSpaceRef 颜色空间;
无效*位图数据;
整数位图字节计数;
int bitmapBytesPerRow;
// 获取图片的宽度,高度。我们将使用整个图像。
size_t 像素宽度 = CGImageGetWidth(inImage);
size_t 像素高 = CGImageGetHeight(inImage);
// 声明每行的字节数。此位图中的每个像素
//例子用4个字节表示;红、绿、蓝和各 8 位
// α。
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelHigh);
// 使用通用 RGB 颜色空间。
colorSpace = CGColorSpaceCreateDeviceRGB();//CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
如果(颜色空间 == NULL)
{
fprintf(stderr, "分配色彩空间时出错\n");
返回空值;
}
// 为图像数据分配内存。这是记忆中的目的地
// 任何绘制到位图上下文的地方都会被渲染。
bitmapData = malloc(bitmapByteCount);
如果(位图数据 == NULL)
{
fprintf (stderr, "内存未分配!");
CGColorSpaceRelease(颜色空间);
返回空值;
}
// 创建位图上下文。我们想要预乘 ARGB,8 位
// 每个组件。无论源图像格式是什么
//(CMYK、灰度等)它将被转换为格式
// 这里由 CGBitmapContextCreate 指定。
上下文 = CGBitmapContextCreate(位图数据,
像素宽,
像素高,
8, // 每个组件的位数
bitmapBytesPerRow,
色彩空间,
kCGImageAlphaPremultipliedFirst);
如果(上下文 == NULL)
{
免费(位图数据);
fprintf (stderr, "上下文未创建!");
}
// 在返回之前确保并释放颜色空间
CGColorSpaceRelease(颜色空间);
返回上下文;
}
看看 github 上的 OBSHAPED 按钮。它将解决您的问题快乐编码。:)