【问题标题】:Why does this code resizing an image rotate it 90 degrees from original? [duplicate]为什么调整图像大小的这段代码会将其从原始图像旋转 90 度? [复制]
【发布时间】:2013-04-23 18:21:30
【问题描述】:

我有一个使用相机的 iPad 应用程序。原始图像为 480 x 640。我正在尝试将其调整为 124 x 160,然后使用我在互联网上找到的以下代码将其存储在 CoreData 中:

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {

CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = image.CGImage;

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

CGContextConcatCTM(context, flipVertical);

// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);

// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

CGImageRelease(newImageRef);
UIGraphicsEndImageContext();

return newImage;

}

图像被逆时针旋转 90 度返回给我,我不明白为什么。我试过注释掉这个声明:

CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

但这没什么区别。这里有什么问题?

【问题讨论】:

  • 图像的旋转不是元数据的属性,而不是像素的实际重新对齐吗?至少我认为它适用于使用 iPod/iPhone/iPad 的相机(以及许多其他消费类相机)拍摄的照片
  • 靠复制粘贴代码sn-p生存的人死于复制粘贴代码sn-p。这里要做的就是思考。 UIImage 有方向信息。 CGImage 没有。因此,您将这些信息丢弃并且永远不会恢复它。
  • 好的...我已经尝试了这里建议的所有内容,但没有任何帮助...我明白马特在说什么,但这无济于事...我只想节省空间通过缩放图像...似乎这是一项收效甚微的大量工作...
  • UIImage 的方向是否已设置,如果设置,则设置为什么?
  • 不,这是 iPad 相机在纵向模式下拍摄的原始 UIImage。

标签: ios objective-c uiimage


【解决方案1】:

感谢所有提出建议的人。我一直在寻找并找到了这个,它可以缩放并保持方向:

    CGRect screenRect = CGRectMake(0, 0, 120.0, 160.0);
    UIGraphicsBeginImageContext(screenRect.size);
    [image drawInRect:screenRect blendMode:kCGBlendModePlusDarker alpha:1];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

【讨论】:

    【解决方案2】:

    这样绘制时需要考虑图像的方向。检查这个答案iOS - UIImageView - how to handle UIImage image orientation

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 2015-12-28
      • 2012-07-12
      • 2012-10-26
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多