在UIImage和Base64字符串之间转换

有谁知道如何将a转换UIImage为Base64字符串,然后将其反转?

我有以下代码;编码之前的原始图像是好的,但是编码和解码后我只能得到空白图像。

NSData *imageData = UIImagePNGRepresentation(viewImage);

NSString *b64EncStr = [self encode: imageData];

NSString *base64String = [self encodeBase64:imageData];

回答:

回答:

//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)!

let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)

let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!

let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!

let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)

print(strBase64)

let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!

let decodedimage:UIImage = UIImage(data: dataDecoded)!

print(decodedimage)

yourImageView.image = decodedimage

let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!

let decodedimage = UIImage(data: dataDecoded)

yourImageView.image = decodedimage


回答:

回答:

您可以使用 NSData'sbase64EncodedStringWithOptions

- (NSString *)encodeToBase64String:(UIImage *)image {

return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

}

- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {

NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];

return [UIImage imageWithData:data];

}


回答:

:使用此链接到encodedecode图像

在您的中添加Base64类project

 NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);

NSString *strEncoded = [Base64 encode:data];

 NSData* data = [Base64 decode:strEncoded ];;

image.image = [UIImage imageWithData:data];

:将QSUtilities用于encodingdecoding


以上是 在UIImage和Base64字符串之间转换 的全部内容, 来源链接: utcz.com/qa/408810.html

回到顶部