2

我在 iPad 上有一个 png 图像,尺寸为 1214x1214(对于视网膜来说是两倍),并将其设置为位于屏幕坐标 (0,-20) 处的 UIImageView。为了在设备旋转/方向更改期间适合屏幕,我将其设置为 Aspect Fit 类型。

我想做的是能够触摸屏幕并读取我触摸下方像素的 RGB 值。我已经实现了一个 UIGestureRecognizer 并将其绑定到 UIImage 并且成功地恢复了触摸坐标。

给我带来麻烦的是我已经尝试实现几种检索 RGB 值的方法(例如 [如何获取 iphone 上图像上像素的 RGB 值])1 但我的 RGB 值看起来好像图像倾斜并映射到 UIView 上的不同位置。

我的问题是,我如何才能满足我已将 UIImageView 设置为 Aspect Fit 的事实以及设备可能处于横向或纵向(倒置或正向上)的事实?

4

1 回答 1

1

好的,所以我解决了,这可能对尝试做类似事情的人有所帮助。

我使用这个函数从另一个答案计算了图像的缩放大小

-(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;
  }

然后根据图像的原始大小及其 Aspect Fit 大小重新缩放

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);

似乎工作得很好,希望它有用。如果我做错了什么,欢迎评论!

于 2012-06-13T19:49:47.143 回答