NSDataBase64DecodingOptions

NSDataBase64DecodingOptions

本文介绍了NSDataBase64DecodingOptions将返回零值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析的base64类型分为为imageData。但我的NSDataBase64DecodingOptions返回nil。我印德codedData,它是零。我的code是这里

  VAR base64String = arrayText [auto_type_android_img]为String的println(basestring64是\\(base64String))让德codedData =的NSData(base64En codedString:base64String,选择:NSDataBase64DecodingOptions(rawValue:0))的println(基本字符串:\\(DE codedData))
如果让德codeDIMAGE = UIImage的(数据:德codedData){    imageTypeOfCar?=图像配德codeDIMAGE
}


解决方案

base64String

 数据:图像/ PNG; BASE64,iVBORw0KGgoA ...... YII =

最初的数据:图像/ PNG; BASE64,终止的的为Base64的一部分
编码和必须字符串前除去可德codeD:

 如果再让范围= base64String.rangeOfString(数据:图像/ PNG; BASE64,选项:.AnchoredSearch){
    base64String.removeRange(范围)
}

然后

 让德codedData =的NSData(base64En codedString:base64String,选择:无)
如果让德codeDIMAGE = UIImage的(数据:德codedData){
    imageTypeOfCar?=图像配德codeDIMAGE
}

成功并显示

I am trying to parse base64 type into imageData. But my NSDataBase64DecodingOptions returns nil. I printed decodedData and it is nil. My code is here

var base64String = arrayText["auto_type_android_img"] as String

println("basestring64 is \(base64String)")

let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0))

println("base string is: \(decodedData)")
if let decodedImage = UIImage(data: decodedData!) {

    imageTypeOfCar?.image = decodedImage
}
解决方案

Your base64String is

The initial is not part of the Base64encoding and must be removed before the string can be decoded:

if let range = base64String.rangeOfString("data:image/png;base64,", options: .AnchoredSearch)  {
    base64String.removeRange(range)
}

Then

let decodedData = NSData(base64EncodedString: base64String, options: nil)
if let decodedImage = UIImage(data: decodedData!) {
    imageTypeOfCar?.image = decodedImage
}

succeeds and displays

这篇关于NSDataBase64DecodingOptions将返回零值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 16:10