【问题标题】:Image from UIIMagePicker gets rotated来自 UIIMagePicker 的图像被旋转
【发布时间】:2012-11-23 14:06:04
【问题描述】:

我需要从 uiimagepicker 获取图像并需要在 uiimageview 中显示它。它在模拟器中完美运行。但是在设备上,来自 uiimagepicker 的图像被旋转了。如何解决这个问题?

【问题讨论】:

  • 重新旋转到原来的样子

标签: ios ios5 ios6


【解决方案1】:

由于手机摄像头的纵向/横向方向,会出现此问题。像

一样重新旋转您的图像
CGImageRef imageRef = [sourceImage CGImage];

UIImage *rotatedImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationUp];

或者试试这样的方法

static inline double radians (double degrees) {return degrees * M_PI/180;}
UIImage* rotate(UIImage* src, UIImageOrientation orientation)
{
    UIGraphicsBeginImageContext(src.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orientation == UIImageOrientationRight) {
        CGContextRotateCTM (context, radians(90));
    } else if (orientation == UIImageOrientationLeft) {
        CGContextRotateCTM (context, radians(-90));
    } else if (orientation == UIImageOrientationDown) {
        // NOTHING
    } else if (orientation == UIImageOrientationUp) {
        CGContextRotateCTM (context, radians(90));
    }

    [src drawAtPoint:CGPointMake(0, 0)];

    return UIGraphicsGetImageFromCurrentImageContext();
}

【讨论】:

    【解决方案2】:

    在使用之前修复图像的方向。 使用下面的扩展来修复 UIImage 方向。

    extension UIImage {
        
        /// Fix image orientaton to protrait up
        func fixedOrientation() -> UIImage? {
            guard imageOrientation != UIImage.Orientation.up else {
                // This is default orientation, don't need to do anything
                return self.copy() as? UIImage
            }
            
            guard let cgImage = self.cgImage else {
                // CGImage is not available
                return nil
            }
            
            guard let colorSpace = cgImage.colorSpace, let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
                return nil // Not able to create CGContext
            }
            
            var transform: CGAffineTransform = CGAffineTransform.identity
            
            switch imageOrientation {
            case .down, .downMirrored:
                transform = transform.translatedBy(x: size.width, y: size.height)
                transform = transform.rotated(by: CGFloat.pi)
            case .left, .leftMirrored:
                transform = transform.translatedBy(x: size.width, y: 0)
                transform = transform.rotated(by: CGFloat.pi / 2.0)
            case .right, .rightMirrored:
                transform = transform.translatedBy(x: 0, y: size.height)
                transform = transform.rotated(by: CGFloat.pi / -2.0)
            case .up, .upMirrored:
                break
            @unknown default:
                break
            }
            
            // Flip image if needed to, this is to prevent flipped image
            switch imageOrientation {
            case .upMirrored, .downMirrored:
                transform = transform.translatedBy(x: size.width, y: 0)
                transform = transform.scaledBy(x: -1, y: 1)
            case .leftMirrored, .rightMirrored:
                transform = transform.translatedBy(x: size.height, y: 0)
                transform = transform.scaledBy(x: -1, y: 1)
            case .up, .down, .left, .right:
                break
            @unknown default:
                break
            }
            
            ctx.concatenate(transform)
            
            switch imageOrientation {
            case .left, .leftMirrored, .right, .rightMirrored:
                ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
            default:
                ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
                break
            }
            
            guard let newCGImage = ctx.makeImage() else { return nil }
            return UIImage.init(cgImage: newCGImage, scale: 1, orientation: .up)
        }
    }
    

    用法:

    let fixedImage = myImage.fixedOrientation()
    

    【讨论】:

    • 这完全没有必要。 OP 可能将图像保存为 PNG 并丢弃其方向。所需要的只是使用 JPEG 而不是 PNG。顺便说一句,为什么不简单地 return self 而不是 return self.copy() as? UIImage
    猜你喜欢
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2018-08-22
    • 1970-01-01
    相关资源
    最近更新 更多