2017-08-10 81 views
0

我正在使用UIImagePickerController從圖庫和相機中選擇圖像。我正在使用以下代表UIImagePickerController圖像類型在swift 3中

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) { 
    imageView = info[UIImagePickerControllerEditedImage] as! UIImage 
    profilePhoto.contentMode = .scaleAspectFill 
    profilePhoto.image = imageView 
    let imageData: Data = UIImagePNGRepresentation(profilePhoto.image!)! 
    self.base64String = imageData.base64EncodedString() 
    dismiss(animated:true, completion: nil) 
    } 

    public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 
    print("Cancel") 
    } 

我正在成功設置配置文件視圖中的圖像。但我需要知道通過畫廊和相機選擇的圖像格式,即是否需要將類型發送到後端,這是.jpeg還是.png。請不要告訴你怎麼知道圖像

+0

我建議是更好的決定格式/更容易讓後端推斷圖像的類型(.jpeg或.png)。如果你必須**發送一個類型,我的建議是總是發送'.png',這兩個都適用(jpeg不支持透明)。 – zero

回答

0

的相機,你可以使用 讓jpgImage = UIImageJPEGRepresentation(ImageView的,1.0) 和 讓pngImage = UIImagePNGRepresentation(ImageView的)