我在NSString中有一个base64图像数据。我想在UIWebView中直接显示它。我怎样才能做到这一点?
到目前为止我所知道的是这样的:
NSString * imageStr = /* some base64 image string */
NSData * imageData = [[NSData alloc]initWithBase64EncodedString:imageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
[webView loadData:imageData
MIMEType:@"image/jpeg;base64"
textEncodingName:@"utf-8"
baseURL:[NSURL URLWithString:@""]];
但它并没有在UIWebView上显示任何内容。问题是什么?感谢。
到目前为止我的参考资料:
答案 0 :(得分:0)
尝试这样:
NSString * imageStr = [self encodeToBase64String:[UIImage imageNamed:@"image_name"]];
NSData *data = [[NSData alloc]initWithBase64EncodedString:imageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
[_webview loadData:data
MIMEType:@"image/jpg"
textEncodingName:@"utf-8"
baseURL:[NSURL URLWithString:@""]];
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
答案 1 :(得分:0)
将您的MIMEType更改为" image / jpg"它会工作。
NSString * imageStr = /* some base64 image string */
NSData * imageData = [[NSData alloc]initWithBase64EncodedString:imageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
- [webView loadData:imageData
MIMEType:@"image/jpeg;base64"
+ [webView loadData:imageData
MIMEType:@"image/jpg"
textEncodingName:@"utf-8"
baseURL:[NSURL URLWithString:@""]];