gpt4 book ai didi

ios - 如何获取UIImage在特定点击点的RGBA值

转载 作者:行者123 更新时间:2023-11-29 10:59:18 26 4
gpt4 key购买 nike

我正在开发一个在 viewcontoller 中有多个 UIImageViews 的 iPad 应用程序。每个图像都有一些透明部分。当用户点击图像时我想测试他点击的图像区域是否不透明然后我想做一些 Action

经过搜索我得出结论,我必须访问图像的原始数据并检查用户点击的点的 alpha 值

我使用了在 here 中找到的解决方案它帮助了很多。我修改了代码,如果用户点击的点是透明的(alpha <1)然后打印 0 否则打印 1。但是,结果在运行时不准确。我有时会得到 0,其中单击的点不透明,反之亦然。我认为 byteIndex 值有问题我不确定它是否会返回用户单击时的颜色数据

这是我的代码

   CGPoint touchPoint;
- (void)viewDidLoad
{
[super viewDidLoad];
[logo addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]];
}

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

UITouch *touch = [[event allTouches] anyObject];
touchPoint = [touch locationInView:self.view];

}
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{

int x = touchPoint.x;
int y = touchPoint.y;

[self getRGBAsFromImage:img atX:x andY:y];
}

- (void)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy {

// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
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 * yy) + xx * bytesPerPixel;


CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
byteIndex += 4;
if (alpha < 1) {
NSLog(@"0");
// here I should add the action I want

}
else NSLog(@"1");



free(rawData);

}

提前谢谢你

最佳答案

这种方法很糟糕,因为它每次都绘制完整的图像,并且还依赖于精确的字节布局。

我建议只绘制到 1x1 上下文中并获取它的 alpha


- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
int x = touchPoint.x;
int y = touchPoint.y;

CGFloat alpha = [self getAlphaFromImage:img atX:x andY:y];
if(alpha<1)

}

- (CGFloat)getAlphaFromImage:(UIImage*)image atX:(NSInteger)xx andY:(NSInteger)yy {
// Cancel if point is outside image coordinates
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), CGPointMake(xx,yy))) {
    return 0;
}

// Create a 1x1 pixel byte array and bitmap context to draw the pixel into.
// Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage
NSInteger pointX = xx;
NSInteger pointY = yy;
CGImageRef cgImage = self.CGImage;
NSUInteger width = self.size.width;
NSUInteger height = self.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * 1;
NSUInteger bitsPerComponent = 8;
unsigned char pixelData[4] = { 0, 0, 0, 0 };
CGContextRef context = CGBitmapContextCreate(pixelData,
                                             1,
                                             1,
                                             bitsPerComponent,
                                             bytesPerRow,
                                             colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy);

// Draw the pixel we are interested in onto the bitmap context
CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
CGContextRelease(context);

CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
return alpha;
}

关于ios - 如何获取UIImage在特定点击点的RGBA值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16722926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com