Swift PNG 图像以不正确的方向保存
如果我在保存之前使用图像,这是正常的。但如果我保存它并稍后使用它,它就会旋转 90 度。我怎样才能确保它不会横向保存?
func saveEvent(_ center1: CLLocation, title2: String, imagePicked1: UIImage)
{
let data = UIImagePNGRepresentation(imagePicked1);///
let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(NSUUID().uuidString+".dat")
do {
try data!.write(to: url!, options: [])
} catch let e as NSError {
print("Error! \(e)");
return
}
let image11 = CKAsset(fileURL: url!)
self.eventRecord.setObject(image11 as CKAsset, forKey: "Picture")
let publicData = CKContainer.default().publicCloudDatabase
publicData.save(self.eventRecord, completionHandler: { record, error in
if error == nil
{
print("Image saved")
}else{
print(error!)
}
})
}
If I use the image before it is saved it is normal. But if I save it and use it later is is 90 degrees turned. How can I make sure it doesn't save sideways?
func saveEvent(_ center1: CLLocation, title2: String, imagePicked1: UIImage)
{
let data = UIImagePNGRepresentation(imagePicked1);///
let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(NSUUID().uuidString+".dat")
do {
try data!.write(to: url!, options: [])
} catch let e as NSError {
print("Error! \(e)");
return
}
let image11 = CKAsset(fileURL: url!)
self.eventRecord.setObject(image11 as CKAsset, forKey: "Picture")
let publicData = CKContainer.default().publicCloudDatabase
publicData.save(self.eventRecord, completionHandler: { record, error in
if error == nil
{
print("Image saved")
}else{
print(error!)
}
})
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要以正确的旋转保存 PNG,并且方向不是
.up
,则需要重新绘制图像。您可以按如下方式重绘它:编辑/更新:
对于 iOS10+ tvOS10+,您可以使用
UIGraphicsImageRenderer
:Playground 测试:
不透明图像的用法:
具有透明度:
If you need to save your PNG with correct rotation you will need to redraw your image if its orientation it is not
.up
. You can redraw it as follow:edit/update:
For iOS10+ tvOS10+ you can use
UIGraphicsImageRenderer
:Playground testing:
Usage for images without transparency:
With transparency :
您也可以使用它来防止它改变方向。
You can use this as well to prevent it from changing of orientation.
只需将图像转换为 JPEG 数据即可。无需重绘图像:
let imageData = image.jpegData(compressionQuality: 1.0)
Just convert the image to JPEG data instead. No need to redraw your image:
let imageData = image.jpegData(compressionQuality: 1.0)