12

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!)
                }
        })
    }
5
  • Just use JPEG and your rotation will be fine
    – Leo Dabus
    Commented Feb 7, 2017 at 19:34
  • how can I do that @LeoDabus
    – Steve
    Commented Feb 7, 2017 at 19:34
  • yes or save it as JPEG @LeoDabus
    – Steve
    Commented Feb 7, 2017 at 19:36
  • stackoverflow.com/questions/29726643/…
    – Leo Dabus
    Commented Feb 7, 2017 at 19:39
  • 1
    JPEG worked well thanks
    – Steve
    Commented Feb 7, 2017 at 19:57

3 Answers 3

25

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:

extension UIImage {
    func png(isOpaque: Bool = true) -> Data? { flattened(isOpaque: isOpaque)?.pngData() }
    func flattened(isOpaque: Bool = true) -> UIImage? {
        if imageOrientation == .up { return self }
        UIGraphicsBeginImageContextWithOptions(size, isOpaque, scale)
        defer { UIGraphicsEndImageContext() }
        draw(in: CGRect(origin: .zero, size: size))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

edit/update:

For iOS10+ tvOS10+ you can use UIGraphicsImageRenderer:

extension UIImage {
    func png(isOpaque: Bool = true) -> Data? { flattened(isOpaque: isOpaque).pngData() }
    func flattened(isOpaque: Bool = true) -> UIImage {
        if imageOrientation == .up { return self }
        let format = imageRendererFormat
        format.opaque = isOpaque
        return UIGraphicsImageRenderer(size: size, format: format).image { _ in draw(at: .zero) }
    }
}

Playground testing:

Usage for images without transparency:

let image = UIImage(data: try! Data(contentsOf: URL(string: "https://i.sstatic.net/varL9.jpg")!))!

if let data = image.png() {
    let imageFromPNGData = UIImage(data: data)
}

With transparency :

if let data = image.png(isOpaque: false) {
    let imageFromPNGData = UIImage(data: data)
}
3
  • 2
    This is the only answer that worked for me, I searched for days. Thank you very much. Commented Mar 14, 2018 at 22:06
  • This is amazing, but I'm having a job converting this flattened data back to a UIImage with UIImage(data: . This method seems to reject the png data. Do you know of a solution?
    – Geoff H
    Commented Nov 15, 2018 at 19:55
  • @GeoffH This is a new image context. You can save it as JPEG or PNG. Feel free to open a new question with a minimum verifiable example I will take a look at it if you wish
    – Leo Dabus
    Commented Nov 15, 2018 at 23:44
2

You can use this as well to prevent it from changing of orientation.

func rotateImage(image: UIImage) -> UIImage? {
    if (image.imageOrientation == UIImage.Orientation.up ) {
        return image
    }
    UIGraphicsBeginImageContext(image.size)
    image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
    let copy = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return copy
}
0
2

Just convert the image to JPEG data instead. No need to redraw your image:

let imageData = image.jpegData(compressionQuality: 1.0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.