Reputation: 31
While assigning a decoded image to imageview, the image becomes incorrectly oriented.
This is how its assigned to an imageview..
@IBAction func imageClicked(_ sender: Any) {
if let decodedData = Data(base64Encoded: result, options: .ignoreUnknownCharacters) {
let image = UIImage(data: decodedData)
myImageView.image = image
}
}
It's not that the image has a wrong orientation always. Sometimes the orientation is fine while sometimes its oriented incorrectly. How can I fix this..? Went through many SO posts..but didn't get the desired results yet..
EDIT 1 This is how I compress it to base64
let imageData = image.jpegData(compressionQuality: 0.6)
let compressedJPGImage = UIImage(data: imageData!)
let img_Data = compressedJPGImage?.toBase64()
It is this image data that I'm saving to db.
Upvotes: 0
Views: 322
Reputation: 236340
Your issue is probably in your toBase64 method. It is probably converting your image to PNG data representation before encoding it. You can simply use Data's method base64EncodedString()
directly in your JPEG data to encode its representation into a base 64 encoded string.
if let result = image.jpegData(compressionQuality: 0.6)?.base64EncodedString() {
// save your string to your database
}
Upvotes: 1