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!) } }) } 

One Solution collect form web for “Swift PNG图像以不正确的方向保存”

如果你需要保存你的PNG正确的旋转,你将需要重新绘制图像,如果其方向不是.up 。 您可以重新绘制如下:

 extension UIImage { var png: Data? { guard let flattened = flattened else { return nil } return UIImagePNGRepresentation(flattened) } var flattened: UIImage? { if imageOrientation == .up { return self } UIGraphicsBeginImageContextWithOptions(size, false, scale) defer { UIGraphicsEndImageContext() } draw(in: CGRect(origin: .zero, size: size)) return UIGraphicsGetImageFromCurrentImageContext() } } 
  • UIImagePickerController摄像头视图在iOS 8上奇怪的旋转(图片)
  • presentModalViewController:Animated已在ios6中弃用
  • UIImage在iPhone上用文件名保存图像
  • iPhone:相机预览覆盖
  • 自定义视图的相机
  • iOS 8快照未呈现的视图会导致一个空的快照
  • 使用UIImagePickerController时,iOS 10错误 <私人>
  • 裁剪UIImage