0

I am trying to create a SwiftUI app that, in one of its functions, saves an image as its pngdata to a class.

class Coupon: Codable, Equatable, Identifiable {
    var id = UUID()
    var data: [String]
    var date: Date
    var image: Data
    init(name: String, date: String, dateobject: Date, image: UIImage, description: String) {
        self.data = [name, date, description]
        self.date = dateobject
        self.image = image.pngData()!
}
    static func == (one: Coupon, two: Coupon) -> Bool {
        return one.data == two.data && one.date == two.date
}
}

But whenever I display the image, like this:

Image(uiImage: UIImage(data: coupon.image))

Certain images get rotated upside down.

I've looked in many different answers, but none seem to solve my problem.

Why is this happening, and how do I solve it?

Kunal Katiyar
  • 304
  • 2
  • 15

1 Answers1

2

I solved my own problem by digging around. The problem was that I was saving the UIImage as pngdata, which makes some images get a simpler new orientation. By simply saving it as jpegdata, by doing this:

self.image = image.jpegData(compressionQuality: 1) ?? Data()

I was able to save the orientation, save some space (because JPEGs are smaller), and save my app!

Kunal Katiyar
  • 304
  • 2
  • 15