iOS - Image Orientation before upload on server in Swift 5.0?
You take a photo from your app then its display in photo view as portrait. but whenever you upload this photo on server that isn't have original orientation of photo which you show. So the image from the server get 90 degree rotate of original portrait image.
We are creating the extension for the image orientation in swift 5. So Adding the below extension of UIImage on your code.
Extension for UIImage Orientation in Swift 5.
How To Use :
Before upload image on server you have to orientation. when you take image where you are orientation image. By use of the above extension we can rotate image.
iOS - Image Orientation before upload on server in Swift 3.0?
Thanks.
We are creating the extension for the image orientation in swift 5. So Adding the below extension of UIImage on your code.
Extension for UIImage Orientation in Swift 5.
extension UIImage {
/// Fix image orientaton to protrait up
func fixedOrientation() -> UIImage? {
guard imageOrientation != UIImage.Orientation.up else {
// This is default orientation, don't need to do anything
return self.copy() as? UIImage
}
guard let cgImage = self.cgImage else {
// CGImage is not available
return nil
}
guard let colorSpace = cgImage.colorSpace, let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
return nil // Not able to create CGContext
}
var transform: CGAffineTransform = CGAffineTransform.identity
switch imageOrientation {
case .down, .downMirrored:
transform = transform.translatedBy(x: size.width, y: size.height)
transform = transform.rotated(by: CGFloat.pi)
case .left, .leftMirrored:
transform = transform.translatedBy(x: size.width, y: 0)
transform = transform.rotated(by: CGFloat.pi / 2.0)
case .right, .rightMirrored:
transform = transform.translatedBy(x: 0, y: size.height)
transform = transform.rotated(by: CGFloat.pi / -2.0)
case .up, .upMirrored:
break
@unknown default:
fatalError("Missing...")
break
}
// Flip image one more time if needed to, this is to prevent flipped image
switch imageOrientation {
case .upMirrored, .downMirrored:
transform = transform.translatedBy(x: size.width, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
case .leftMirrored, .rightMirrored:
transform = transform.translatedBy(x: size.height, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
case .up, .down, .left, .right:
break
@unknown default:
fatalError("Missing...")
break
}
ctx.concatenate(transform)
switch imageOrientation {
case .left, .leftMirrored, .right, .rightMirrored:
ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
default:
ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
break
}
guard let newCGImage = ctx.makeImage() else { return nil }
return UIImage.init(cgImage: newCGImage, scale: 1, orientation: .up)
}
}
How To Use :
Before upload image on server you have to orientation. when you take image where you are orientation image. By use of the above extension we can rotate image.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
var image = info[UIImagePickerControllerOriginalImage] as! UIImage
image = image. fixedOrientation();
}
iOS - Image Orientation before upload on server in Swift 3.0?
Thanks.
iOS - Image Orientation before upload on server in Swift 5.0?
Reviewed by KIRIT MODI
on
21:08:00
Rating:
No comments: