Encoding/Decoding base64 解码数据 = 无
Encoding/Decoding base64 decodedData = nil
昨天我为此写了一个工作代码,但我删除了它,当写一个新的代码时,有些东西真的很奇怪:
我这样编码图片:
let pictureData = UIImagePNGRepresentation(picture!)
let pictureToString64 = pictureData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
(我之前有 JPEGRepresentation,但它不起作用,所以我尝试使用 JPEG)
我通过获取 pic 64
来解码这种方式,我相信它具有正确的值。它以 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")
问题是 decodedData 总是 nil。 为什么会这样?
我认为这与 NSDataBase64DecodingOptions
有关。
使用 NSDataBase64DecodingOptions.IgnoreUnknownCharacters
而不是 NSDataBase64DecodingOptions(rawValue: 0)
,我能够将 Base64 编码的字符串解码回 NSData
,并从该数据创建一个 UIImage
:
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 初始值设定项。最好使用其中一种集合类型,而不是传入一个值。我在 header 中看到 NSDataBase64DecodingOptions
是结构中唯一的 public var,所以我试了一下。
@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 }
}
昨天我为此写了一个工作代码,但我删除了它,当写一个新的代码时,有些东西真的很奇怪:
我这样编码图片:
let pictureData = UIImagePNGRepresentation(picture!)
let pictureToString64 = pictureData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
(我之前有 JPEGRepresentation,但它不起作用,所以我尝试使用 JPEG)
我通过获取 pic 64
来解码这种方式,我相信它具有正确的值。它以 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")
问题是 decodedData 总是 nil。 为什么会这样?
我认为这与 NSDataBase64DecodingOptions
有关。
使用 NSDataBase64DecodingOptions.IgnoreUnknownCharacters
而不是 NSDataBase64DecodingOptions(rawValue: 0)
,我能够将 Base64 编码的字符串解码回 NSData
,并从该数据创建一个 UIImage
:
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 初始值设定项。最好使用其中一种集合类型,而不是传入一个值。我在 header 中看到 NSDataBase64DecodingOptions
是结构中唯一的 public var,所以我试了一下。
@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 }
}