的UIImage和Base64编码字符串之间的转换 [英] Convert between UIImage and Base64 string
问题描述
有谁知道如何转换的UIImage
来一个Base64字符串,然后反转呢?
我有低于code;原始图像编码之前是好的,但我后,我带code和德code,只得到一个空白的形象。
的NSData *为imageData = UIImagePNGRe presentation(viewImage);* NSString的b64EncStr = [自我EN code:为imageData];* NSString的base64String = [自我EN codeBase64:为imageData];
斯威夫特
首先,我们需要有图像的NSData的
从包创建的NSData //使用图像名称
让图像:UIImage的=的UIImage(命名为:imageNameHere)!
//现在使用的图像创建成NSData的格式
让我们为imageData:NSData的= UIImagePNGRe presentation(图)!//或NEXT可能性//使用图片的路径创建的NSData
让网址:NSURL = NSURL(字符串:urlHere)!
//现在使用的图像创建成NSData的格式
让我们为imageData:NSData的= NSData.init(contentsOfURL:URL)!
雨燕2.0>编码
让strBase64:字符串= imageData.base64En codedStringWithOptions(.Encoding64CharacterLineLength)
雨燕2.0>解码
让dataDe codeD:NSData的NSData的=(base64En codedString:strBase64,选择:NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
编码
让strBase64 = imageData.base64En codedStringWithOptions(.allZeros)
打印(strBase64)
解码
让dataDe codeD:NSData的NSData的=(base64En codedString:strBase64,选择:NSDataBase64DecodingOptions(rawValue:0))!
让德codeDIMAGE:UIImage的=的UIImage(数据:dataDe codeD)!
打印(德codeDIMAGE)
yourImageView.image =去codeDIMAGE
的Objective-C
iOS7>版本
您可以使用的NSData的
base64En codedStringWithOptions
编码
- (的NSString *)EN codeToBase64String:(*的UIImage)图像{
返回[UIImagePNGRe presentation(图)base64En codedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
解码
- (*的UIImage)德codeBase64ToImage:(* NSString的)STREN $ C $ {CDATA
NSData的*数据= [[NSData的页头] initWithBase64En codedString:STREN codeDATA选项:NSDataBase64DecodingIgnoreUnknownCharacters];
返回[UIImage的imageWithData:数据]
}
的iOS 6.1和<版本
第一个选项:使用的此链接连接code
和德code
图片
类在你的项目
。
编码
的NSData *数据= UIImageJPEGRe presentation(yourImage,1.0F);
* NSString的STREN codeD = [Base64编码的连接code:数据]
解码
的NSData *数据= [Base64编码德code:STREN codeD] ;;
image.image = [UIImage的imageWithData:数据]
另一种选择:使用 QSUtilities 为编码
和解码
Does anyone know how to convert a UIImage
to a Base64 string, and then reverse it?
I have the below code; the original image before encoding is good, but I only get a blank image after I encode and decode it.
NSData *imageData = UIImagePNGRepresentation(viewImage);
NSString *b64EncStr = [self encode: imageData];
NSString *base64String = [self encodeBase64:imageData];
Swift
First we need to have image's NSData
//Use image name from bundle to create NSData
let image : UIImage = UIImage(named:"imageNameHere")!
//Now use image to create into NSData format
let imageData:NSData = UIImagePNGRepresentation(image)!
//OR next possibility
//Use image's path to create NSData
let url:NSURL = NSURL(string : "urlHere")!
//Now use image to create into NSData format
let imageData:NSData = NSData.init(contentsOfURL: url)!
Swift 2.0 > Encoding
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
Swift 2.0 > Decoding
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
Encoding :
let strBase64 = imageData.base64EncodedStringWithOptions(.allZeros)
print(strBase64)
Decoding :
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
let decodedimage:UIImage = UIImage(data: dataDecoded)!
print(decodedimage)
yourImageView.image = decodedimage
Objective-C
iOS7 > version
You can use NSData's
base64EncodedStringWithOptions
Encoding :
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
Decoding :
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
iOS 6.1 and < version
First Option : Use this link to encode
and decode
image
Add Base64 class in your project
.
Encoding :
NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);
NSString *strEncoded = [Base64 encode:data];
Decoding :
NSData* data = [Base64 decode:strEncoded ];;
image.image = [UIImage imageWithData:data];
Another Option: Use QSUtilities for encoding
and decoding
这篇关于的UIImage和Base64编码字符串之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!