2014-03-30 156 views
0

我已经看过类似的问题,但仍然没有将Base64编码的字符串正确地转换为UIImage。我使用http://www.freeformatter.com/base64-encoder.html来测试我的字符串,并可以成功获取图像作为回应。将Base64字符串转换为UIImage

但是,当我使用initWithBase64EncodedString时,我得到一个无响应。下面是我正在使用的代码以及我用来测试的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

回答

2

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

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

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

您的代码表示dataFromBase64String没有已知的方法简单。 – Muju

+0

@Muju我的答案是使用外部库,我更新包括链接。 – bmjohns

+0

对不起,我还没有看到你的整个答案。你的答案解决了我的问题。非常感谢你。 – Muju

1

在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]; 
}