Swift PNG Image being saved with incorrect orientation









up vote
5
down vote

favorite
3












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

)










share|improve this question























  • Just use JPEG and your rotation will be fine
    – Leo Dabus
    Feb 7 '17 at 19:34











  • how can I do that @LeoDabus
    – Steve
    Feb 7 '17 at 19:34










  • yes or save it as JPEG @LeoDabus
    – Steve
    Feb 7 '17 at 19:36










  • stackoverflow.com/questions/29726643/…
    – Leo Dabus
    Feb 7 '17 at 19:39







  • 1




    JPEG worked well thanks
    – Steve
    Feb 7 '17 at 19:57














up vote
5
down vote

favorite
3












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

)










share|improve this question























  • Just use JPEG and your rotation will be fine
    – Leo Dabus
    Feb 7 '17 at 19:34











  • how can I do that @LeoDabus
    – Steve
    Feb 7 '17 at 19:34










  • yes or save it as JPEG @LeoDabus
    – Steve
    Feb 7 '17 at 19:36










  • stackoverflow.com/questions/29726643/…
    – Leo Dabus
    Feb 7 '17 at 19:39







  • 1




    JPEG worked well thanks
    – Steve
    Feb 7 '17 at 19:57












up vote
5
down vote

favorite
3









up vote
5
down vote

favorite
3






3





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

)










share|improve this question















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

)







ios swift uiimage png uiimagepickercontroller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 7 '17 at 20:29









Leo Dabus

127k29257332




127k29257332










asked Feb 7 '17 at 19:32









Steve

2151322




2151322











  • Just use JPEG and your rotation will be fine
    – Leo Dabus
    Feb 7 '17 at 19:34











  • how can I do that @LeoDabus
    – Steve
    Feb 7 '17 at 19:34










  • yes or save it as JPEG @LeoDabus
    – Steve
    Feb 7 '17 at 19:36










  • stackoverflow.com/questions/29726643/…
    – Leo Dabus
    Feb 7 '17 at 19:39







  • 1




    JPEG worked well thanks
    – Steve
    Feb 7 '17 at 19:57
















  • Just use JPEG and your rotation will be fine
    – Leo Dabus
    Feb 7 '17 at 19:34











  • how can I do that @LeoDabus
    – Steve
    Feb 7 '17 at 19:34










  • yes or save it as JPEG @LeoDabus
    – Steve
    Feb 7 '17 at 19:36










  • stackoverflow.com/questions/29726643/…
    – Leo Dabus
    Feb 7 '17 at 19:39







  • 1




    JPEG worked well thanks
    – Steve
    Feb 7 '17 at 19:57















Just use JPEG and your rotation will be fine
– Leo Dabus
Feb 7 '17 at 19:34





Just use JPEG and your rotation will be fine
– Leo Dabus
Feb 7 '17 at 19:34













how can I do that @LeoDabus
– Steve
Feb 7 '17 at 19:34




how can I do that @LeoDabus
– Steve
Feb 7 '17 at 19:34












yes or save it as JPEG @LeoDabus
– Steve
Feb 7 '17 at 19:36




yes or save it as JPEG @LeoDabus
– Steve
Feb 7 '17 at 19:36












stackoverflow.com/questions/29726643/…
– Leo Dabus
Feb 7 '17 at 19:39





stackoverflow.com/questions/29726643/…
– Leo Dabus
Feb 7 '17 at 19:39





1




1




JPEG worked well thanks
– Steve
Feb 7 '17 at 19:57




JPEG worked well thanks
– Steve
Feb 7 '17 at 19:57












1 Answer
1






active

oldest

votes

















up vote
10
down vote



accepted










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 
var png: Data?
guard let flattened = flattened else return nil
return flattened.pngData() // Swift 4.2 or later
// return UIImagePNGRepresentation(flattened) // Swift 4.1 or earlier

var flattened: UIImage?
if imageOrientation == .up return self
UIGraphicsBeginImageContextWithOptions(size, false, scale)
defer UIGraphicsEndImageContext()
draw(in: CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()







share|improve this answer


















  • 1




    This is the only answer that worked for me, I searched for days. Thank you very much.
    – Alisson Enz
    Mar 14 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
    Nov 15 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
    2 days ago










  • this works for me let image = UIImage(data: try! Data(contentsOf: URL(string: "https://i.stack.imgur.com/varL9.jpg")!))! let pngData = image.png! let uiImage = UIImage(data: pngData)
    – Leo Dabus
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42098390%2fswift-png-image-being-saved-with-incorrect-orientation%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
10
down vote



accepted










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 
var png: Data?
guard let flattened = flattened else return nil
return flattened.pngData() // Swift 4.2 or later
// return UIImagePNGRepresentation(flattened) // Swift 4.1 or earlier

var flattened: UIImage?
if imageOrientation == .up return self
UIGraphicsBeginImageContextWithOptions(size, false, scale)
defer UIGraphicsEndImageContext()
draw(in: CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()







share|improve this answer


















  • 1




    This is the only answer that worked for me, I searched for days. Thank you very much.
    – Alisson Enz
    Mar 14 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
    Nov 15 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
    2 days ago










  • this works for me let image = UIImage(data: try! Data(contentsOf: URL(string: "https://i.stack.imgur.com/varL9.jpg")!))! let pngData = image.png! let uiImage = UIImage(data: pngData)
    – Leo Dabus
    2 days ago















up vote
10
down vote



accepted










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 
var png: Data?
guard let flattened = flattened else return nil
return flattened.pngData() // Swift 4.2 or later
// return UIImagePNGRepresentation(flattened) // Swift 4.1 or earlier

var flattened: UIImage?
if imageOrientation == .up return self
UIGraphicsBeginImageContextWithOptions(size, false, scale)
defer UIGraphicsEndImageContext()
draw(in: CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()







share|improve this answer


















  • 1




    This is the only answer that worked for me, I searched for days. Thank you very much.
    – Alisson Enz
    Mar 14 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
    Nov 15 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
    2 days ago










  • this works for me let image = UIImage(data: try! Data(contentsOf: URL(string: "https://i.stack.imgur.com/varL9.jpg")!))! let pngData = image.png! let uiImage = UIImage(data: pngData)
    – Leo Dabus
    2 days ago













up vote
10
down vote



accepted







up vote
10
down vote



accepted






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 
var png: Data?
guard let flattened = flattened else return nil
return flattened.pngData() // Swift 4.2 or later
// return UIImagePNGRepresentation(flattened) // Swift 4.1 or earlier

var flattened: UIImage?
if imageOrientation == .up return self
UIGraphicsBeginImageContextWithOptions(size, false, scale)
defer UIGraphicsEndImageContext()
draw(in: CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()







share|improve this answer














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 
var png: Data?
guard let flattened = flattened else return nil
return flattened.pngData() // Swift 4.2 or later
// return UIImagePNGRepresentation(flattened) // Swift 4.1 or earlier

var flattened: UIImage?
if imageOrientation == .up return self
UIGraphicsBeginImageContextWithOptions(size, false, scale)
defer UIGraphicsEndImageContext()
draw(in: CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()








share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 22 at 13:44

























answered Feb 7 '17 at 19:56









Leo Dabus

127k29257332




127k29257332







  • 1




    This is the only answer that worked for me, I searched for days. Thank you very much.
    – Alisson Enz
    Mar 14 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
    Nov 15 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
    2 days ago










  • this works for me let image = UIImage(data: try! Data(contentsOf: URL(string: "https://i.stack.imgur.com/varL9.jpg")!))! let pngData = image.png! let uiImage = UIImage(data: pngData)
    – Leo Dabus
    2 days ago













  • 1




    This is the only answer that worked for me, I searched for days. Thank you very much.
    – Alisson Enz
    Mar 14 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
    Nov 15 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
    2 days ago










  • this works for me let image = UIImage(data: try! Data(contentsOf: URL(string: "https://i.stack.imgur.com/varL9.jpg")!))! let pngData = image.png! let uiImage = UIImage(data: pngData)
    – Leo Dabus
    2 days ago








1




1




This is the only answer that worked for me, I searched for days. Thank you very much.
– Alisson Enz
Mar 14 at 22:06




This is the only answer that worked for me, I searched for days. Thank you very much.
– Alisson Enz
Mar 14 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
Nov 15 at 19:55




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
Nov 15 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
2 days ago




@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
2 days ago












this works for me let image = UIImage(data: try! Data(contentsOf: URL(string: "https://i.stack.imgur.com/varL9.jpg")!))! let pngData = image.png! let uiImage = UIImage(data: pngData)
– Leo Dabus
2 days ago





this works for me let image = UIImage(data: try! Data(contentsOf: URL(string: "https://i.stack.imgur.com/varL9.jpg")!))! let pngData = image.png! let uiImage = UIImage(data: pngData)
– Leo Dabus
2 days ago


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42098390%2fswift-png-image-being-saved-with-incorrect-orientation%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo