繁体   English   中英

将Base64字符串转换为UIImage

[英]Converting a Base64 String into a UIImage

我已经看过这种类型问题的类似答案,但仍不足以将Base64编码的字符串正确转换为UIImage。 我已经使用http://www.freeformatter.com/base64-encoder.html来测试我的字符串,并且可以成功获取图像作为响应。

但是,当我使用initWithBase64EncodedString时,得到的响应为nil。 以下是我正在使用的代码以及用于测试的Base64字符串的示例。 我在这里想念什么?

码:

imageData = [[NSData alloc] initWithBase64EncodedString:checkBytesString options:0]; checkImage = [UIImage imageWithData:imageData];

字符串(它很大,因此我通过OneDrive共享它):

https://onedrive.live.com/redir?resid=60AA391B8FEA9C36!107&authkey=!AFK_y5UHOFsdYsKZI&ithint=file%2c.rtf

答案是使用外部库NSData + Base64。 它实现了dataFromBase64String方法,该方法正确返回了imageData,因此可以将其转换为图像。

https://github.com/l4u/NSData-Base64

imageData = [NSData dataFromBase64String:frontCheckBytesString];
checkImage = [UIImage imageWithData:imageData];

从iOS 7开始简单

- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData
{
    NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
    return [UIImage imageWithData:data];
}

UIImage *zeroImage = [[UIImage alloc]init];
if(![strb64Image isEqualToString:@""] && strb64Image)
{
    zeroImage = [self decodeBase64ToImage:strB64Image];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM