gpt4 book ai didi

ios - 将 JPEG 图像压缩并复制到文件夹会丢失其旋转 - iOS Swift

转载 作者:搜寻专家 更新时间:2023-11-01 06:23:24 25 4
gpt4 key购买 nike

我目前在我的应用程序文件夹中保存了一张 JPEG 图像,其方向正确。我需要将该图像压缩并复制到一个临时文件夹,然后将 URL 发送到 S3,我这样做是有效的,但图像失去了旋转属性。

这是我的:

//I retrieve the image
PHImageManager.defaultManager().requestImageForAsset(p, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFill, options: nil, resultHandler: {(result, info)in
//Create temp url
let testFileURL = NSURL(fileURLWithPath: NSTemporaryDirectory().stringByAppendingPathComponent("temp"))

//Compress the image, I think is here where the orientation is lost
let data = UIImageJPEGRepresentation(result, 0)

//Write the image to the tmp folder, or maybe it is here where is lost
data.writeToURL(testFileURL!, atomically: true)

//Send URL to upload to S3
self.uploadImagesToS3(testFileURL!, picName: p.valueForKey("filename") as String, deviceID: deviceID, length: data.length)
})

有人知道我能做什么吗?。图像在压缩和复制之前具有正确的方向,但在之后旋转。图片取自相机。

谢谢!

最佳答案

这最终对我有用:

我改变了这一行:

let data = UIImageJPEGRepresentation(result, 0)

对于这个:

let data = UIImageJPEGRepresentation(self.correctlyOrientedImage(result), 0)

并添加了函数:

func correctlyOrientedImage(image: UIImage) -> UIImage {
if image.imageOrientation == UIImageOrientation.Up {
return image
}

UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
var normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return normalizedImage;
}

您可以在这里找到 https://stackoverflow.com/a/27775741/2364453

关于ios - 将 JPEG 图像压缩并复制到文件夹会丢失其旋转 - iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29269814/

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