ios - 缩放后在 UIImageView 内的位置查找 RGB 值

标签 ios ipad uiimageview orientation

我在 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/

相关文章:

ios - 在 Nib 文件中添加 UICollectionView 时应用程序崩溃

ios - 带 NSAttributedString 的首字下沉

ios - HTML标记会使JSON文件无效吗?

modalviewcontroller - 在 iPad 中使用 PresentModalViewController 时出现问题

ios - UIImageView Tap Gesture 在第二次点击时不起作用

ios - 让 UIImageView 上升然后下降 Swift

ios - 如何在支持 iOS 6 的情况下获得类似于 iOS 7 主屏幕的视差运动偏移?

ios - 如果我在没有 SKAction 的情况下直接修改 SKSpriteNode 的位置和旋转,SpriteKit 会应用物理吗?

iphone - 从 AVFoundation Capture Session 获取原始数据流

iphone - UIImageView 更改背景颜色和 setImage 在动画期间不起作用