I created this UIImage extension to fix this problem using the UIImagePNGRe view based on this answer . Therefore, I suggest using this func UIImage.PNGRepresentation(img: UIImage) class instead of the UIKit func UIImagePNGRview.
Swift 3 Code:
// MyUIImage.swift // MyEasyMovie-Public-App // // Created by Ahmed Zahraz on 19/12/2016. // Copyright © 2016 AhmedZahraz. All rights reserved. import Foundation import UIKit extension UIImage { public class func PNGRepresentation(_ img: UIImage) -> Data? { // No-op if the orientation is already correct if (img.imageOrientation == UIImageOrientation.up) { return UIImagePNGRepresentation(img); } // We need to calculate the proper transformation to make the image upright. // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. var transform:CGAffineTransform = CGAffineTransform.identity if (img.imageOrientation == UIImageOrientation.down || img.imageOrientation == UIImageOrientation.downMirrored) { transform = transform.translatedBy(x: img.size.width, y: img.size.height) transform = transform.rotated(by: CGFloat(M_PI)) } if (img.imageOrientation == UIImageOrientation.left || img.imageOrientation == UIImageOrientation.leftMirrored) { transform = transform.translatedBy(x: img.size.width, y: 0) transform = transform.rotated(by: CGFloat(M_PI_2)) } if (img.imageOrientation == UIImageOrientation.right || img.imageOrientation == UIImageOrientation.rightMirrored) { transform = transform.translatedBy(x: 0, y: img.size.height); transform = transform.rotated(by: CGFloat(-M_PI_2)); } if (img.imageOrientation == UIImageOrientation.upMirrored || img.imageOrientation == UIImageOrientation.downMirrored) { transform = transform.translatedBy(x: img.size.width, y: 0) transform = transform.scaledBy(x: -1, y: 1) } if (img.imageOrientation == UIImageOrientation.leftMirrored || img.imageOrientation == UIImageOrientation.rightMirrored) { transform = transform.translatedBy(x: img.size.height, y: 0); transform = transform.scaledBy(x: -1, y: 1); } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. let ctx:CGContext = CGContext(data: nil, width: Int(img.size.width), height: Int(img.size.height), bitsPerComponent: img.cgImage!.bitsPerComponent, bytesPerRow: 0, space: img.cgImage!.colorSpace!, bitmapInfo: img.cgImage!.bitmapInfo.rawValue)! ctx.concatenate(transform) if (img.imageOrientation == UIImageOrientation.left || img.imageOrientation == UIImageOrientation.leftMirrored || img.imageOrientation == UIImageOrientation.right || img.imageOrientation == UIImageOrientation.rightMirrored ) { ctx.draw(img.cgImage!, in: CGRect(x:0,y:0,width:img.size.height,height:img.size.width)) } else { ctx.draw(img.cgImage!, in: CGRect(x:0,y:0,width:img.size.width,height:img.size.height)) } // And now we just create a new UIImage from the drawing context let cgimg:CGImage = ctx.makeImage()! let imgEnd:UIImage = UIImage(cgImage: cgimg) return UIImagePNGRepresentation(imgEnd) } }
AhmedZah Dec 20 '16 at 16:33 2016-12-20 16:33
source share