Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift 3 Image rotated 90 degrees Left

In my app I have a feature to take a picture from camera and upload it to server. My problem is that when I'm getting response back from server that image is rotating to 90 degree left side. Here is my tried code:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    self.PickerFrom = self.PickerFrom2
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
    {
       self.AttachmentImageView.image = pickedImage
    }
    dismiss(animated: true, completion: nil)
    uploadGalleryImage(image: self.AttachmentImageView.image!)
}

func uploadGalleryImage( image:UIImage)
{
    let imageData: NSData = UIImagePNGRepresentation(image)! as NSData
    let base64String = imageData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
    self.AttachedDocumentURL = "data:image/png;base64,\(base64String)"
}

AttachedDocumentURL contains base64sting of captured image.

like image 962
Kavin Kumar Arumugam Avatar asked Jul 26 '17 10:07

Kavin Kumar Arumugam


2 Answers

Use this common function to fix upside image orientation from any.

extension UIImage {

    func fixImageOrientation() -> UIImage? {


        if (self.imageOrientation == .up) {
            return self
        }

        var transform: CGAffineTransform = CGAffineTransform.identity


        if ( self.imageOrientation == .left || self.imageOrientation == .leftMirrored ) {
            transform = transform.translatedBy(x: self.size.width, y: 0)
            transform = transform.rotated(by: CGFloat(Double.pi / 2.0))
        } else if ( self.imageOrientation == .right || self.imageOrientation == .rightMirrored ) {
            transform = transform.translatedBy(x: 0, y: self.size.height);
            transform = transform.rotated(by: CGFloat(-Double.pi / 2.0));
        } else if ( self.imageOrientation == .down || self.imageOrientation == .downMirrored ) {
            transform = transform.translatedBy(x: self.size.width, y: self.size.height)
            transform = transform.rotated(by: CGFloat(Double.pi))
        } else if ( self.imageOrientation == .upMirrored || self.imageOrientation == .downMirrored ) {
            transform = transform.translatedBy(x: self.size.width, y: 0)
            transform = transform.scaledBy(x: -1, y: 1)
        } else if ( self.imageOrientation == .leftMirrored || self.imageOrientation == .rightMirrored ) {
            transform = transform.translatedBy(x: self.size.height, y: 0);
            transform = transform.scaledBy(x: -1, y: 1);
        }


        if let context: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height),
                                       bitsPerComponent: self.cgImage!.bitsPerComponent, bytesPerRow: 0,
                                       space: self.cgImage!.colorSpace!,
                                       bitmapInfo: self.cgImage!.bitmapInfo.rawValue) {

            context.concatenate(transform)

            if ( self.imageOrientation == UIImageOrientation.left ||
                self.imageOrientation == UIImageOrientation.leftMirrored ||
                self.imageOrientation == UIImageOrientation.right ||
                self.imageOrientation == UIImageOrientation.rightMirrored ) {
                context.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.height,height: self.size.width))
            } else {
                context.draw(self.cgImage!, in: CGRect(x: 0,y: 0,width: self.size.width,height: self.size.height))
            }

            if let contextImage = context.makeImage() {
                return UIImage(cgImage: contextImage)
            }

        }

        return nil
    }
}
like image 71
Krunal Avatar answered Nov 14 '22 01:11

Krunal


Swift 4.1 compatible. This extension avoids sending image exif data on the iOS device side.

extension UIImage {

    func fixOrientation() -> UIImage? {

        if (imageOrientation == .up) { return self }

        var transform = CGAffineTransform.identity

        switch imageOrientation {
        case .left, .leftMirrored:
            transform = transform.translatedBy(x: size.width, y: 0.0)
            transform = transform.rotated(by: .pi / 2.0)
        case .right, .rightMirrored:
            transform = transform.translatedBy(x: 0.0, y: size.height)
            transform = transform.rotated(by: -.pi / 2.0)
        case .down, .downMirrored:
            transform = transform.translatedBy(x: size.width, y: size.height)
            transform = transform.rotated(by: .pi)
        default:
            break
        }

        switch imageOrientation {
        case .upMirrored, .downMirrored:
            transform = transform.translatedBy(x: size.width, y: 0.0)
            transform = transform.scaledBy(x: -1.0, y: 1.0)
        case .leftMirrored, .rightMirrored:
            transform = transform.translatedBy(x: size.height, y: 0.0)
            transform = transform.scaledBy(x: -1.0, y: 1.0)
        default:
            break
        }

        guard let cgImg = cgImage else { return nil }

        if let context = CGContext(data: nil,
                                   width: Int(size.width), height: Int(size.height),
                                   bitsPerComponent: cgImg.bitsPerComponent,
                                   bytesPerRow: 0, space: cgImg.colorSpace!,
                                   bitmapInfo: cgImg.bitmapInfo.rawValue) {

            context.concatenate(transform)

            if imageOrientation == .left || imageOrientation == .leftMirrored ||
                imageOrientation == .right || imageOrientation == .rightMirrored {
                context.draw(cgImg, in: CGRect(x: 0.0, y: 0.0, width: size.height, height: size.width))
            } else {
                context.draw(cgImg, in: CGRect(x: 0.0 , y: 0.0, width: size.width, height: size.height))
            }

            if let contextImage = context.makeImage() {
                return UIImage(cgImage: contextImage)
            }

        }

        return nil
    }

}
like image 3
m4f1h4rd Avatar answered Nov 13 '22 23:11

m4f1h4rd