在NSString中解码Base-64编码的PNG

时间:2013-05-23 02:54:11

标签: ios cocoa-touch base64 nsdata

我有一些NSData是Base-64编码的,我想解码它,我看到了一个看起来像这样的例子

NSData* myPNGData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];

[Base64 initialize];
NSData *data = [Base64 decode:img];
cell.image.image = [UIImage imageWithData:myPNGData];

然而,这给了我一大堆错误,我想知道该怎么做才能让它发挥作用。我需要将某种类型的文件导入到我的项目中,还是必须包含一个框架?

这些是我得到的错误

Use of undeclared identifier 'Base64'
Use of undeclared identifier 'Base64'
Use of undeclared identifier 'cell'

我到处寻找,无法弄清楚做什么是正确的。

6 个答案:

答案 0 :(得分:12)

您可以将Base64编码的字符串解码为NSData

-(NSData *)dataFromBase64EncodedString:(NSString *)string{
    if (string.length > 0) {

        //the iPhone has base 64 decoding built in but not obviously. The trick is to
        //create a data url that's base 64 encoded and ask an NSData to load it.
        NSString *data64URLString = [NSString stringWithFormat:@"data:;base64,%@", string];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:data64URLString]];
        return data;
    }
    return nil;
}

使用上述方法从Base64字符串中获取图像的示例:

-(void)imageFromBase64EncodedString{

    NSString *string = @"";  // replace with encocded string
    NSData *imageData = [self dataFromBase64EncodedString:string];
    UIImage *myImage = [UIImage imageWithData:imageData];

    // do something with image
}

答案 1 :(得分:10)

NSData Base64 library files会帮助你。

#import "NSData+Base64.h"

//Data from your string is decoded & converted to UIImage
UIImage *image = [UIImage imageWithData:[NSData
dataFromBase64String:strData]];

希望有所帮助

Swift 3版本

它几乎相同

//Create your NSData object
let data = NSData(base64Encoded: "yourStringData", options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)

//And then just create a new image based on the data object
let image = UIImage(data: data as! Data)

Swift 2.3版本

//Create your NSData object
let data = NSData(base64EncodedString: "yourStringData", options: .IgnoreUnknownCharacters)

//And then just create a new image based on the data object
let image = UIImage(data: data!)

答案 2 :(得分:7)

//retrieve your string 

NSString *string64 = //... some string base 64 encoded

//convert your string to data

NSData *data = [[NSData alloc] initWithBase64EncodedString:string64 options:NSDataBase64DecodingIgnoreUnknownCharacters];

//initiate image from data

UIImage *captcha_image = [[UIImage alloc] initWithData:data];

答案 3 :(得分:1)

 NSString *base64String=@"............";
    NSData* data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
            UIImage* img = [UIImage imageWithData:data];

//imageview  is the refrence outlet of image view
            self.imageview.image=img;

答案 4 :(得分:0)

这适用于ios7及以上版本。

Exception in thread "main" java.lang.ClassCastException: src.Test cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at src.Test.main(Test.java:14)

并使用这样的代码:

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

答案 5 :(得分:0)

Swift 3版本

使用类型数据而不是NSData

let data = Data(base64Encoded: imageBase64String, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)
let image = UIImage(data:data!)