如何在 UIImageView 中显示 base64 图像?

How to display a base64 image within a UIImageView?(如何在 UIImageView 中显示 base64 图像?)
本文介绍了如何在 UIImageView 中显示 base64 图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个 Base64 gif 图片:

I got this Base64 gif image:

R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7

现在我想在我的 iPhone 应用程序中显示这个 Base64 字符串.

Now I want to display this Base64 String within my IPhone App.

我可以通过使用 WebView 来完成这项工作:

I could get this working by using the WebView:

aUIWebView.scalesPageToFit = NO;
aUIWebView.opaque = NO;
aUIWebView.backgroundColor = [UIColor clearColor]; 
[aUIWebView loadHTMLString:
  @"<html><body style=""background-color: transparent""><img src=""data:image/png;base64,R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7"" /></body></html>"
 baseURL:nil];

是否可以通过使用 UIImageView 来做同样的事情?

Is it possible to do the same thing by using the UIImageView?

推荐答案

您不必对其进行编码.只需制作一个 NSUrl,它就知道data:"-url.

You don't have to encode it. Simply make a NSUrl, it knows the "data:"-url.

NSURL *url = [NSURL URLWithString:base64String];    
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *ret = [UIImage imageWithData:imageData];

正如评论中提到的,您必须确保在数据前添加 data:image/png;base64, 否则您的 base64 数据将毫无用处.

As mentioned in the comments, you have to make sure that you prepend your data with data:image/png;base64, or else your base64 data is useless.

这篇关于如何在 UIImageView 中显示 base64 图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How to stop UIBarButtonItem text from truncating?(如何阻止UIBarButtonItem文本被截断?)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
How to send an image with custom keyboard iOS(如何使用自定义键盘IO发送映像)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)