有很多类似的问题,但没有他们帮助我,所以我只是不知道该怎么办,但问。所以这就是我编码UIImage的方式:
let data: NSData = UIImagePNGRepresentation(imageResized)!
let base64String: NSString = data.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
和解码(如建议here):
if let range = base64.rangeOfString("data:image/png;base64,", options: .AnchoredSearch) {
base64.removeRange(range)
}
let decodedData = NSData(base64EncodedString: base64, options: NSDataBase64DecodingOptions(rawValue: 0))
if let decodedImage = UIImage(data: decodedData!) {
self.imageList.append(decodedImage)
}
但初始化decodeData时应用程序崩溃,我无法弄清楚原因。 我检查了我的base64字符串here并返回了图片。 任何帮助将非常感谢!
答案 0 :(得分:1)
试试这个:
if let decodedData = NSData(base64EncodedString: base64, options:NSDataBase64DecodingOptions.IgnoreUnknownCharacters){
if(decodedData.length > 0){
let imageDecodeData = UIImage(data: decodedData)!
}
else{
print("error")
}
}
答案 1 :(得分:0)
将let decodedData
替换为:
let decodedData = NSData(base64EncodedString: base64, options: [])
并确保使用正确的变量:
// 1. This should be a variable if you're mutating it.
let base64String = data...
// 2. You should probably do this on your base64String
if let range = base64.rangeOfString(...)