来自相机的 UIImage 旋转 90 度 - 使用扩展 [英] UIImage from camera is rotated 90 degrees - using extensions

查看:31
本文介绍了来自相机的 UIImage 旋转 90 度 - 使用扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当返回图像数据并应用于 UIImage 时,如果数据来自相机,则图像显示为旋转 90 度.

When Image data is returned and applied to a UIImage, if the data comes from the camera then the image appears rotated 90 degrees.

我尝试添加

extension UIImage {
func correctlyOrientedImage() -> UIImage {
    if self.imageOrientation == UIImageOrientation.up {
        return self
    }

    UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
    self.draw(in: CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width:  self.size.width, height: self.size.height))
)
     let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext();

    return normalizedImage;
}
}

在我的代码中,我检查是否有为特定用户保存的数据,如果有,我将图像数据加载到 profile_image,一个 UIImageView.

in my code, I check to see if I have data saved for a specific user and if so I load the image data into profile_image, a UIImageView.

    //profile_image
    if let profile_imager = UserDefaults.standard.object(forKey: String(UserID)) as? Data {
       let data = UserDefaults.standard.object(forKey: String(UserID)) as? Data
        print("profile_imager: \(profile_imager)")
        profile_image.image = UIImage(data: data!)
        profile_image.backgroundColor = .white
    }

我将如何正确使用OrientedImage

How would I go about to use correctlyOrientedImage correctly

谢谢

推荐答案

Swift 4.2,

创建了一个UIImage扩展来保持图片在原来的位置,

Created an UIImage extension to keep the image in the original position,

extension UIImage {
    func fixedOrientation() -> UIImage {

        if imageOrientation == .up {
            return self
        }

        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)
        case .right, .rightMirrored:
            transform = transform.translatedBy(x: 0, y: size.height)
            transform = transform.rotated(by: CGFloat.pi / -2)
        case .up, .upMirrored:
            break
        }

        switch imageOrientation {
        case .upMirrored, .downMirrored:
            transform.translatedBy(x: size.width, y: 0)
            transform.scaledBy(x: -1, y: 1)
        case .leftMirrored, .rightMirrored:
            transform.translatedBy(x: size.height, y: 0)
            transform.scaledBy(x: -1, y: 1)
        case .up, .down, .left, .right:
            break
        }

        if let cgImage = self.cgImage, let colorSpace = cgImage.colorSpace,
            let ctx: CGContext = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) {
            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))
            }
            if let ctxImage: CGImage = ctx.makeImage() {
                return UIImage(cgImage: ctxImage)
            } else {
                return self
            }
        } else {
            return self
        }
    }
}

然后从fixedOrientation()方法设置profile_image

//profile_image
if let profile_imager = UserDefaults.standard.object(forKey: String(UserID)) as? Data {
   let data = UserDefaults.standard.object(forKey: String(UserID)) as? Data
    print("profile_imager: \(profile_imager)")
    let actualImage = UIImage(data: data!)?.fixedOrientation()
    profile_image.image = actualImage
    profile_image.backgroundColor = .white
}

这篇关于来自相机的 UIImage 旋转 90 度 - 使用扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆