iOS Swift 3 Image rotated 90 degrees Left

后端 未结 3 1344
北海茫月
北海茫月 2021-01-02 15:14

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 r

相关标签:
3条回答
  • 2021-01-02 15:50

    Try this (reset you image orientation) before you upload on server

    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)
    
        if let updatedImage = self.AttachmentImageView.image?.updateImageOrientionUpSide() {
          uploadGalleryImage(image: updatedImage)
        } else {
          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)"
    }
    
    // Image extension
    extension UIImage {
    
        func updateImageOrientionUpSide() -> UIImage? {
            if self.imageOrientation == .up {
                return self
            }
    
            UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
            self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
            if let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() {
                UIGraphicsEndImageContext()
                return normalizedImage
            }
            UIGraphicsEndImageContext()
            return nil
        }
    }
    
    0 讨论(0)
  • 2021-01-02 15:50

    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
        }
    }
    
    0 讨论(0)
  • 2021-01-02 16:01

    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
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题