首先我们需要有图像的NSData
//Use image name from bundle to create NSDatalet image : UIImage = UIImage(named:"imageNameHere")!//Now use image to create into NSData formatlet imagedata:NSData = UIImagePNGRepresentation(image)!//OR next possibility//Use image's path to create NSDatalet url:NSURL = NSURL(string : "urlHere")!//Now use image to create into NSData formatlet imagedata:NSData = NSData.init(contentsOfURL: url)!
Swift 2.0 >编码
let strbase64:String = imageData.base64EnpredStringWithOptions(.Encoding64CharacterLineLength)
Swift 2.0 >解码
let dataDepred:NSData = NSData(base64EnpredString: strbase64, options: NSDatabase64DecodingOptions.IgnoreUnknownCharacters)!
Swift 3.0 >解码
let dataDepred : Data = Data(base64Enpred: strbase64, options: .ignoreUnknownCharacters)!
编码方式 :
let strbase64 = imageData.base64EnpredString(options: .lineLength64Characters)print(strbase64)
解码 :
let dataDepred:NSData = NSData(base64EnpredString: strbase64, options: NSDatabase64DecodingOptions(rawValue: 0))!let depredimage:UIImage = UIImage(data: dataDepred)!print(depredimage)yourImageView.image = depredimage
斯威夫特3.0
let dataDepred : Data = Data(base64Enpred: strbase64, options: .ignoreUnknownCharacters)!let depredimage = UIImage(data: dataDepred)yourImageView.image = depredimage
目标CiOS7>版本
您可以使用
NSData's
base64EnpredStringWithOptions
编码方式 :
- (NSString *)enpreTobase64String:(UIImage *)image { return [UIImagePNGRepresentation(image) base64EnpredStringWithOptions:NSDatabase64Encoding64CharacterLineLength];}
解码 :
- (UIImage *)deprebase64ToImage:(NSString *)strEnpreData { NSData *data = [[NSData alloc]initWithbase64EnpredString:strEnpreData options:NSDatabase64DecodingIgnoreUnknownCharacters]; return [UIImage imageWithdata:data];}
iOS 6.1和<版本
第一种选择 :使用此链接到
enpre和
depre图像
在您的中添加base64类
project。
编码方式 :
NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f); NSString *strEnpred = [base64 enpre:data];
解码 :
NSData* data = [base64 depre:strEnpred ];; image.image = [UIImage imageWithdata:data];
另一个选择
:将QSUtilities用于
encoding和
decoding
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)