gpt4 book ai didi

ios - UIImagePNGRepresentation() 旋转我的 UIImage

转载 作者:行者123 更新时间:2023-11-28 08:51:04 26 4
gpt4 key购买 nike

图片来自UIImagePickerViewController:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
print("1: \(image)")
if let data = UIImagePNGRepresentation(image) {
print("2: \(UIImage(data: data))")
}
}
}

结果如下:

1: size {3024, 4032} orientation 3 scale 1.000000
2: Optional( size {4032, 3024} orientation 0 scale 1.000000)

为什么 UIImagePNGRepresentation() 改变图像的方向?如何预防?

最佳答案

在第一种情况下,您的图像方向是 UIImageOrientationRight(值 3)。在第二种情况下,方向是 UIImageOrientationUp 默认值 0。

您可以在将图像转换为数据时修复方向问题,反之亦然。

请看这个答案 iOS UIImagePickerController result image orientation after upload

更新:您可以使用上面链接中的代码更改修复其方向。不会影响上传过程。

如果不介意数据格式,可以转为JPEG格式

like if let data = UIImageJPEGRepresentation(image, 1.0) {
print("2: \(UIImage(data: data))")
}

关于ios - UIImagePNGRepresentation() 旋转我的 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34175788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com