Swift Png Image Being Saved With Incorrect Orientation

Swift PNG Image being saved with incorrect orientation

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.stack.imgur.com/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)
}

UIImage.pngData(_:) losing Camera photo orientation

If you save UIImage as a JPEG, this will set the rotation flag.

PNGs do not support a rotation flag, so if you save a UIImage as a PNG, it will be rotated incorrectly and not have a flag set to fix it. So if you want PNGs you must rotate them yourself.

let jpgData = downloadedImage.jpegData(compressionQuality: 1)

To get rotated image you need to draw that.. you can use following extension

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

How to use

let rotatedImage =  downloadedImage.rotateImage()

Save UIImage, Load it in Wrong Orientation

I have faced similar problem and here is how I solved it.

While we save image we need to save its orientation information along with the image ...

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:[myImage imageOrientation] forKey:@"kImageOrientation"];
[imageOrientation release];

And we load image we need to read its orientation information and apply it to the image…

UIImage *tempImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
UIImage *orientedImage= [[UIImage alloc] initWithCGImage: tempImage.CGImage scale:1.0 orientation:imageOrientation];
[tempImage release];

orientedImage is what we need.

Thanks,

Rotate downloaded (image.png) before/after saving?

If you display the UIImage in an image view you can try modifying the orientation.
It is basically the same UIImage but with a flag that is interpreted by UIImageView to rotate the image display.

let rotated = UIImage(cgImage: image.cgImage!, scale: 1, orientation: .right)

If you really want to save a new image to disk, you will need to actually redraw the image. Some info is already available here : How to Rotate a UIImage 90 degrees?

UIImage is rotated 90 degrees when creating from url and set to the pasteboard

I am not sure but I think UIPasteBoard converts your image to PNG and discards its orientation. You can explicitly tell the kind of data you are adding to the pasteboard but I am not sure if this would work for your scenery.

extension Data {
var image: UIImage? { UIImage(data: self) }
}

setting your pasteboard data

UIPasteboard.general.setData(jpegData, forPasteboardType: "public.jpeg")

loading the data from pasteboard

if let pbImage = UIPasteboard.general.data(forPasteboardType: "public.jpeg")?.image {

}

Or Redrawing your image before setting your pasteboard image property

extension UIImage {
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()
}
}


UIPasteboard.general.image = image.flattened()

iOS: Image get rotated 90 degree after saved as PNG representation data

Starting with iOS 4.0 when the camera takes a photo it does not rotate it before saving, it

simply sets a rotation flag in the EXIF data of the JPEG.If you save a UIImage as a JPEG, it

will set the rotation flag.PNGs do not support a rotation flag, so if you save a UIImage as a

PNG, it will be rotated incorrectly and not have a flag set to fix it. So if you want PNG

images you must rotate them yourself, for that check this link.

Doesn't Save Desired Image Orientation

Parse is not doing anything with your image. When you are rotating a JPEG image in UIImage, the JPEG image data is not being rotated. Instead the orientation is stored in its EXIF metadata. When you convert your image to PNG to save on Prase, EXIF metadata is lost as PNGs do not store any orientation metadata information. In order to get the image in the correct orientation and mirroring, set the correct orientation and mirroring properties to your video connection before capturing the image. Alternatively you can store your image as a JPEG instead of a PNG.



Related Topics



Leave a reply



Submit