Swift PNG Image being saved with incorrect orientation
up vote
5
down vote
favorite
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
add a comment |
up vote
5
down vote
favorite
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
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
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
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
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
ios swift uiimage png uiimagepickercontroller
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
add a comment |
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
add a comment |
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()
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 melet 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
add a comment |
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()
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 melet 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
add a comment |
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()
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 melet 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
add a comment |
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()
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()
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 melet 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
add a comment |
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 melet 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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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