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eQFRJCfAAAR2EBeQAT5CQAQgQ3kBUSQnwAAEdhAXkAE+QkAEIEN5AVEkJ8AABHYQF5ABPkJABCBDeQFRJCfAAAR2EBeQAT5CQAQgQ3kBUSQnwAAEdhAXkAE+QkAEIEN5AVEkJ8AABHYQF5ABPkJABCBDeQFRJCfAAAR2EBeQAT5CQAQgQ3kBUSQnwAAEdhAXkAE+QkAEIEN5AVEkJ8AABHYQF5ABPkJABCBDeQFRJCfAAAR2EBeQAT5CQAQgQ3kBUSQnwAAEdhAXkAE+QkAEIEN5AVEkJ8AABHYQF5ABPkJABCBDeQFRJCfAAAR2EBeQAT5CQAQgQ3kBUSQnwAAEdhAXkAE+QkAEIEN5AVEkJ8AABHYQF5ABPkJABCBDeQFRJCfAAAR2EBeQAT5CQAQgQ3kBR7HxwORQVNBhAAAAABJRU5ErkJggg==
我看到数据库字段包含:
\211PNG\015\012\032\012\000\000\000\015IHDR\000\000\000\301\000\000\000\313\010\006\000\000\000\327\331\367\372\000\000\001|iCCPICC Profile\000\000(\221c``*I,(\310aa``\310\315+)\012rwR\210\210\214R`\277\303\300\315\300\303 \304`\305 \236\230\\\\\340\030\020\340\303\200\023|\273\306\300\010\242/\353\202\314J\363\374y\323\246\265|\376\0266\257\231rV%:\270\365\201\001wJjq2\003\003#\007\220\235\234R\234\234\013d\347\000\331:\311\005E%@\366\014 [\267\274\244\000\304>\001d\213\024\001\035\010d\337\001\261\323!\354\017 v\022\230\315\304\002V\023\022\344\014dK\000\331\002I\020\266\006\210\235\016a[\200\330\311\031\211)@\266\007\310.\210\033\300\200\323\303E\301\334\300R\327\221\200\273I\006\2719\2450;@\241\305\223\232\027\032\014r\007\020\3130x0\2700(0\2303\0300X2\35028\226\244V\224\200\024:\347\027T\026e\246g\224(8\002C6U\3019?\267\240\264$\265HG\3013/YOG\301\310\300\320\000\244\016\024g\020\243?\007\201Mg\024;\217\020\313_\310\300`\251\314\300\300\334\203\020K\232\306\300\260}\017\003\203\304)\204\230\312<\006\006~k\006\206m\347\012\022\213\022\341\016g\374\306B\210_\234fl\004a\363810\260\336\373\377\377\263\032\003\003\373$\006\206\277\023\377\377\377\275\350\377\377\277\213\201\366\003\343\354@\016\000$wi\340\214k\022\017\000\000\001\235iTXtXML:com.adobe.xmp\000\000\000\000\000<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 5.4.0">\012 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">\012 <rdf:Description rdf:about=""\012 xmlns:exif="http://ns.adobe.com/exif/1.0/">\012 <exif:PixelXDimension>193</exif:PixelXDimension>\012 <exif:PixelYDimension>203</exif:PixelYDimension>\012 </rdf:Description>\012 </rdf:RDF>\012</x:xmpmeta>\012\321\345>B\000\000\003\344IDATx\001\355\323\001\015\000 \014\0041@\343\374[\201\004\033\3279X\363\267g\346.G ,p\302\277{\235\300\027\020\201!\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005D\220\237\000\000\021\330@^@\004\371\011\000\020\201\015\344\005\036\307\307\003\221ASA\204\000\000\000\000IEND\256B`\202
那应该是以字节数组表示的图像。
我想使用Swift将这个字节数组解析回UIImage。为此,我有以下功能:
func convertImage(image: String) -> UIImage? {
var strings = image.components(separatedBy: ",")
var bytes = [UInt8]()
for i in 0 ... strings.count-1 {
if let signedByte = Int8(strings[i]) {
bytes.append(UInt8(bitPattern: signedByte))
} else {
print("ERROR")
// Do something with this error condition
}
}
let datos: NSData = NSData(bytes: bytes, length: bytes.count)
return UIImage(data: datos as Data) // Note it's optional. Don't force unwrap!!!
}
似乎是在循环这个功能但是&#34; ERROR&#34;最后打印出来。然而,我从未结束过UIImage。
我做错了什么?
答案 0 :(得分:2)
iVBORw0KGgoAAAANSUhEUgAAA...
代表base64
编码的字符串。所以你只需要使用方法
let data = Data(base64Encoded: "iVBORw0KGgoAAAANSUhEUgAAA...")
let image = UIImage(data: data)
希望它能帮到你
答案 1 :(得分:1)
let o = Int8("-127")
print(o.dynamicType) // Optional(<Int8>)
// It's optional, so we need to unwrap it...
if let x = o {
print(x) // -127, as expected
//let b = UInt8(x) // Run time crash
let b = UInt8(bitPattern: x) // 129, as it should be
}
因此你的功能应该是
func convierteImagen(cadenaImagen: String) -> UIImage? {
var strings = cadenaImagen.componentsSeparatedByString(",")
var bytes = [UInt8]()
for i in 0..< strings.count {
if let signedByte = Int8(strings[i]) {
bytes.append(UInt8(bitPattern: signedByte))
} else {
// Do something with this error condition
}
}
let datos: NSData = NSData(bytes: bytes, length: bytes.count)
return UIImage(data: datos) // Note it's optional. Don't force unwrap!!!
}