编码/解码的base64德codedData =零 [英] Encoding/Decoding base64 decodedData = nil

查看:293
本文介绍了编码/解码的base64德codedData =零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我写了一个工作code这一点,但我将其擦除和写入一个新的东西的时候是很奇怪的:

Yesterday I wrote a working code for this, but I erased it and when writing a new one something is really weird:

我带code它的图片是这样的:

I encode it a picture this way:

let pictureData = UIImagePNGRepresentation(picture!)
let pictureToString64 = pictureData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

(我以前有JPEGRe presentation,但它没有工作,所以我试图用JPEG)
我去code这样通过让 PIC 64 ,我相信它有正确的值。它始于 iVBORw0KGgoAAAANSUhEUgAAAUAA (...)

(I had JPEGRepresentation before, but it wasn't working, so I tried with JPEG) and I decode this way by getting pic 64 which I believe it has the correct value. It starts with iVBORw0KGgoAAAANSUhEUgAAAUAA(...)

 let decodedData = NSData(base64EncodedString: pic64, options:NSDataBase64DecodingOptions(rawValue: 0))
 let decodedImage = UIImage(data: decodedData!)
 let pictureDataLocal = UIImageJPEGRepresentation(decodedImage!, 100)
                defaults.setObject(pictureDataLocal, forKey: "profilePicture")

的问题是,德codedData始终是零。为什么会出现这种情况?

The problem is that decodedData is always nil. Why is this happening?

推荐答案

我觉得这与做 NSDataBase64DecodingOptions

使用 NSDataBase64DecodingOptions.IgnoreUnknownCharacters 而不是 NSDataBase64DecodingOptions(rawValue:0),我是能够去code中的Base64编码的连接codeD字符串返回到的NSData ,并创建了一个的UIImage 从数据:

Using NSDataBase64DecodingOptions.IgnoreUnknownCharacters instead of NSDataBase64DecodingOptions(rawValue: 0), I was able to decode the Base64 encoded string back into NSData, and created a UIImage from that data:

let picture = UIImage(named: "myImage")

let pictureData = UIImagePNGRepresentation(picture!)

let pictureToString64 = pictureData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

let decodedData = NSData(base64EncodedString: pictureToString64!, options:NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
let decodedImage = UIImage(data: decodedData!)
let pictureDataLocal = UIImageJPEGRepresentation(decodedImage!, 100)

NSDataBase64DecodingOptions OptionSetType 继承这就是为什么你得到的rawValue初始化。最好是使用该组类型中的一个,而传递的值。我在 NSDataBase64DecodingOptions 是在结构中唯一的公共变种头看见了,所以我尝试了。

NSDataBase64DecodingOptions inherits from OptionSetType which is why you get the rawValue initializer. It is better to use the one of the set types, rather passing in a value. I saw in the header that NSDataBase64DecodingOptions was the only public var in the struct, so I tried it.

@available(iOS 7.0, *)
public struct NSDataBase64DecodingOptions : OptionSetType {
    public init(rawValue: UInt)

    // Use the following option to modify the decoding algorithm so that it ignores unknown non-Base64 bytes, including line ending characters.
    public static var IgnoreUnknownCharacters: NSDataBase64DecodingOptions { get }
}

这篇关于编码/解码的base64德codedData =零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆