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 图像转换为Base64字符串)