objective-c 如何在 UIImageView 中显示 base64 图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1366837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display a base64 image within a UIImageView?
提问by jantimon
I got this Base64 gif image:
我得到了这个 Base64 gif 图像:
R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7
Now I want to display this Base64 String within my IPhone App.
现在我想在我的 iPhone 应用程序中显示这个 Base64 字符串。
I could get this working by using the WebView:
我可以通过使用 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];
Is it possible to do the same thing by using the UIImageView?
是否可以使用 UIImageView 做同样的事情?
回答by masche
You don't have to encode it. Simply make a NSUrl, it knows the "data:"-url.
您不必对其进行编码。只需创建一个 NSUrl,它就知道“data:”-url。
NSURL *url = [NSURL URLWithString:base64String];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *ret = [UIImage imageWithData:imageData];
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.
正如评论中提到的,您必须确保在数据前加上data:image/png;base64,,否则您的 base64 数据将毫无用处。
回答by Jonathan M
Very old question, but as of iOS7 there is a new, much easier way to do so, hence I'm writing it here so future readers can use this.
很老的问题,但从 iOS7 开始,有一种新的、更简单的方法可以做到这一点,因此我在这里写它,以便未来的读者可以使用它。
NSData* data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
UIImage* image = [UIImage imageWithData:data];
Very easy to use, and will not hit the 2048 byte size limit of a URL.
非常易于使用,并且不会达到 URL 的 2048 字节大小限制。
回答by Thorsten Niehues
This Code will display an base64 encoded string as a picture:
此代码将显示一个 base64 编码的字符串作为图片:
NSString *str = @"data:image/jpg;base64,";
str = [str stringByAppendingString:restauInfo.picture];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
restauInfo.picture is an NSString which contains the base64 encoded JPG
restauInfo.picture 是一个 NSString,其中包含 base64 编码的 JPG
In my case the Value from "restauInfo.picture" comes from a database
在我的情况下,“restauInfo.picture”中的值来自数据库
回答by Radford7821
You can do something like the following:
您可以执行以下操作:
(UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
Then call it like this:
然后像这样调用它:
UIImage *image = [self decodeBase64ToImage:dataString];
回答by Kornel
- Decode Base64 to raw binary. You're on your own here. You might find
cStringUsingEncoding:ofNSStringuseful. - Create
NSDatainstance usingdataWithBytes:length: - Use
[UIImage imageWithData:]to load it.
- 将 Base64 解码为原始二进制文件。你一个人在这里。您可能会发现
cStringUsingEncoding:的NSString有用的。 NSData使用创建实例dataWithBytes:length:- 使用
[UIImage imageWithData:]加载它。
回答by jantimon
Thanks porneL
谢谢porneL
I could find a Base64 script using google: Base64.m
我可以使用谷歌找到一个 Base64 脚本: Base64.m
So I modified it a little bit and could get a working test code:
所以我稍微修改了一下,可以得到一个有效的测试代码:
NSString * base64img = @"R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7";
base64Data = [base64img dataUsingEncoding:NSASCIIStringEncoding];
base64Bytes = [base64Data bytes];
mutableData = [NSMutableData dataWithCapacity:[base64Data length]];
lentext = [base64Data length];
while( YES ) {
if( ixtext >= lentext ) break;
ch = base64Bytes[ixtext++];
flignore = NO;
if( ( ch >= 'A' ) && ( ch <= 'Z' ) ) ch = ch - 'A';
else if( ( ch >= 'a' ) && ( ch <= 'z' ) ) ch = ch - 'a' + 26;
else if( ( ch >= '0' ) && ( ch <= '9' ) ) ch = ch - '0' + 52;
else if( ch == '+' ) ch = 62;
else if( ch == '=' ) flendtext = YES;
else if( ch == '/' ) ch = 63;
else flignore = YES;
if( ! flignore ) {
short ctcharsinbuf = 3;
BOOL flbreak = NO;
if( flendtext ) {
if( ! ixinbuf ) break;
if( ( ixinbuf == 1 ) || ( ixinbuf == 2 ) ) ctcharsinbuf = 1;
else ctcharsinbuf = 2;
ixinbuf = 3;
flbreak = YES;
}
inbuf [ixinbuf++] = ch;
if( ixinbuf == 4 ) {
ixinbuf = 0;
outbuf [0] = ( inbuf[0] << 2 ) | ( ( inbuf[1] & 0x30) >> 4 );
outbuf [1] = ( ( inbuf[1] & 0x0F ) << 4 ) | ( ( inbuf[2] & 0x3C ) >> 2 );
outbuf [2] = ( ( inbuf[2] & 0x03 ) << 6 ) | ( inbuf[3] & 0x3F );
for( i = 0; i < ctcharsinbuf; i++ )
[mutableData appendBytes:&outbuf[i] length:1];
}
if( flbreak ) break;
}
}
//Finally I can access my image data:
aUIImageView.image = [UIImage imageWithData: [NSData dataWithData:mutableData]];
回答by Carlos Parada
Objective-C
目标-C
NSString *plainString = @"foo";
Encoding
编码
NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
NSLog(@"%@", base64String);
Decoding
解码
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(@"%@", decodedString);
Option
选项
NSURL *URL = [NSURL URLWithString:
[NSString stringWithFormat:@"data:application/octet-stream;base64,%@",
base64String]];
return [NSData dataWithContentsOfURL:URL];
Swift
迅速
let plainString = "foo"
Encoding
编码
let plainData = (plainString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let base64String = plainData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromRaw(0)!)
println(base64String)
Decoding
解码
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.fromRaw(0)!)
let decodedString = NSString(data: decodedData, encoding: NSUTF8StringEncoding)
println(decodedString) // foo
回答by Julian Król
In my case worked a solution proposed here by @Masche. As I needed it in Swift 2.0so:
就我而言,@Masche 在此提出了一个解决方案。正如我在Swift 2.0 中所需要的那样:
let url = NSURL(string: imageString)
let data = NSData.init(contentsOfURL: url!)
let image = UIImage(data: imageData)
回答by Martin
Just in case anyone is looking for the Swiftcode to accomplish this (based on the Objective-Canswer provided by Jonathan M), here it is:
以防万一有人正在寻找Swift代码来完成此操作(基于Jonathan M 提供的Objective-C答案),这里是:
var data = NSData (base64EncodedString: base64String, options: NSDataBase64DecodingOptions(0))
var image = UIImage(data: data!)
回答by Maverick
Swift 4+
斯威夫特 4+
let base64EncodedString = "" // Your Base64 Encoded String
if let imageData = Data(base64Encoded: base64EncodedString) {
let image = UIImage(data: imageData)
self.imageView.image = image
}

