2016-06-07 88 views
2

以下方法我生病嘗試,但圖像不能正常顯示,它讓損失如何將圖像轉換爲base64而無需在swift 2.0中丟失圖像?

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
    img_View.image = info[UIImagePickerControllerOriginalImage] as? UIImage 
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage 
    let imageData:NSData = UIImagePNGRepresentation(image)! 
    strImage = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength) 
    print(strImage) 
    dismissViewControllerAnimated(true, completion: nil) 
} 

這裏有損圖像Corrupted image here 雖然我我得到完整的圖像略有模糊圖像解碼成我的電腦,但是當我發送我的base64字符串服務(WEBAPI)(Windows PC),他們只得到一半的圖像。有人可以幫助我如何解決這個問題

+0

你是什麼意思「沒有正確顯示」?你的意思是'strImage'不打印出編碼的圖像嗎? – Fonix

+0

@Fonix,他我得到像半像的空間圖像被損壞。 –

+0

也許'Encoding64CharacterLineLength'選項會干擾使用該base64數據的任何內容,請不要嘗試使用任何選項 – Fonix

回答

0

你可能的選項封蓋它CharacterLineLength

我只是這樣做:imageData.base64EncodedDataWithOptions(NSDataBase64EncodingOptions())

0

斯威夫特3

var sample = UIImage(named: "try-swift-logo") 
    let imageData:Data = UIImagePNGRepresentation(sample!)! 
    let base64String = imageData.base64EncodedString()