Swift 图像转换为Base64字符串

前端之家收集整理的这篇文章主要介绍了Swift 图像转换为Base64字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//本地路径文件
let filePath = NSBundle.mainBundle().pathForResource("test",ofType: "jpg")
let data = NSData(contentsOfFile: filePath!)
let base64 = data!.base64EncodedStringWithOptions(.EncodingEndLineWithLineFeed)

//使用图像名称创建NSData
let image = UIImage(named:"imageNameHere")!
let imageData = UIImagePNGRepresentation(image)
let base64 = imageData!.base64EncodedStringWithOptions(.EncodingEndLineWithCarriageReturn)

//创建NSData使用图像的路径
let url = NSURL(string : "urlHere")!
let imageData = NSData(contentsOfURL: url)

//Swift 2.0 Encoding(编码)
let base64String = imageData!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)

//Swift 2.0 Decoding(解码)
let decodedData = NSData(base64EncodedString: base64String,options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)

猜你在找的Swift相关文章