我在 iPad 上有一个 png 图像,尺寸为 1214x1214(视网膜尺寸是它的两倍),并将其设置为位于屏幕坐标 (0,-20) 的 UIImageView。为了在设备旋转/方向更改期间适合屏幕,我将其设置为 Aspect Fit 类型。
我想要做的是能够触摸屏幕并读出触摸下像素的 RGB 值。我已经实现了 UIGestureRecognizer 并将其绑定(bind)到 UIImage,并且成功地获取了触摸坐标。
给我带来麻烦的是,我已经尝试实现几种检索 RGB 值的方法(例如 [如何获取 iphone 上图像上像素的 RGB 值])1 但我的 RGB 值看起来好像图像倾斜并映射到 UIView 上的不同位置。
我的问题是,我如何满足我已将 UIImageView 设置为 Aspect Fit 以及设备可能处于横向或纵向(上下颠倒或垂直向上)的事实?
最佳答案
好的,所以我解决了,这可能对尝试做类似事情的人有帮助。
我使用另一个答案中的这个函数计算了图像的缩放大小
-(CGRect)frameForImage:(UIImage*)image inImageViewAspectFit:(UIImageView*)imageView
{
float imageRatio = image.size.width / image.size.height;
float viewRatio = imageView.frame.size.width / imageView.frame.size.height;
if(imageRatio < viewRatio)
{
float scale = imageView.frame.size.height / image.size.height;
float width = scale * image.size.width;
float topLeftX = (imageView.frame.size.width - width) * 0.5;
return CGRectMake(topLeftX, 0, width, imageView.frame.size.height);
}
else
{
float scale = imageView.frame.size.width / image.size.width;
float height = scale * image.size.height;
float topLeftY = (imageView.frame.size.height - height) * 0.5;
return CGRectMake(0, topLeftY, imageView.frame.size.width, height);
}
}
通过将函数注册为监听器获取接触点
CGPoint tapPoint = [sender locationInView:imageMap];
根据我的图像因 iPad 旋转而移动到的位置更改了触摸点
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ||
[UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown )
{
// portrait (y has increased, x has stayed the same)
tapPoint.y -= rectScaleSize.origin.y;
}
else
{
// landscape (x has increased, y has stayed the same)
tapPoint.x -= rectScaleSize.origin.x;
}
然后根据图像的原始大小及其纵横比大小重新缩放
tapPoint.x = (tapPoint.x * imageMap.image.size.width) / rectScaleSize.size.width;
tapPoint.y = (tapPoint.y * imageMap.image.size.height) / rectScaleSize.size.height;
其中 imageMap.image 是我的原始图像,rectScaleSize 是 frameForImage 函数的返回值
最后得到RGB值
CGImageRef image = [imageMap.image CGImage];
NSUInteger width = CGImageGetWidth(image);
NSUInteger height = CGImageGetHeight(image);
// NSLog(@"RGB Image is %d x %d",width,height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
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),image);
CGContextRelease(context);
int byteIndex = (bytesPerRow * (int)tapPoint.y) + (int)tapPoint.x * bytesPerPixel;
int red = rawData[byteIndex];
int green = rawData[byteIndex + 1];
int blue = rawData[byteIndex + 2];
//int alpha = rawData[byteIndex + 3];
NSLog(@"RGB is %d,%d,%d",red,green,blue);
看起来效果还不错,希望能派上用场。 如果我做错了什么,欢迎评论!
关于ios - 缩放后在 UIImageView 内的位置查找 RGB 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11014010/