2017-07-26 77 views
3

在我的應用程序中,我有一個功能,可以從camera拍攝照片並將其上傳到服務器。我的問題是,當我從服務器獲得迴應時,圖像旋轉到90度左側。這裏是我的嘗試代碼:iOS Swift 3圖像左轉90度

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包含base64sting捕獲的圖像。

+0

圖像如何保存在服務器上,其存儲的在90度或保存在正常 –

+0

當u拿手機的攝像頭,該圖像具有方向屬性本身。因此,請按照該方向檢查並旋轉併發送到服務器。很多可用的網絡代碼都可以自己編寫。 – Ammaiappan

+0

你可以在這裏查看我的答案https://stackoverflow.com/questions/45157225/want-fixed-orientation-but-the-uiimage-autoratate/45157920#45157920 – luckyShubhra

回答

6

試試這個(重置您的圖像方向),您的服務器

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 
    } 
} 
3

使用上傳這個共同的功能從任何修復上攻形象定位之前。

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 
    } 
}