解码Base-64编码的PNG在一个NSString。

时间:2022-10-06 18:34:12

I have some NSData which is Base-64 encoded and I would like to decode it, I have seen an example that looks like this

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

NSData* myPNGData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];

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

However this gives me a load of errors, I would like to know what to do in order to get this to work. Is there some type of file I need to import into my project or do I have to include a framework?

然而,这给了我大量的错误,我想知道如何做才能使它工作。我是否需要将某种类型的文件导入到我的项目中,还是需要包含一个框架?

These are the errors I get

这些是我得到的错误

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

I have looked everywhere and cannot figure out what is the proper thing to do.

我到处都找过了,想不出该做什么。

6 个解决方案

#1


10  

NSData Base64 library files will help you.

NSData Base64库文件将帮助您。

#import "NSData+Base64.h"

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

Hope it helps

希望它能帮助

Swift 3 Version

斯威夫特3版本

It's pretty much the same

几乎是一样的

//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 Version

斯威夫特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


12  

You can decode a Base64 encoded string to NSData:

您可以解码一个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;
}

Example use of above method to get an image from the Base64 string:

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

-(void)imageFromBase64EncodedString{

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

    // do something with image
}

#3


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

#4


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;

#5


0  

This will work for ios7 and above.

这将适用于ios7和以上版本。

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

And use the code like this:

使用如下代码:

UIImage *img = [self decodeBase64ToImage:<ur base64 string>];

#6


0  

Swift 3 version

Use type Data instead of NSData

使用类型数据代替NSData

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

#1


10  

NSData Base64 library files will help you.

NSData Base64库文件将帮助您。

#import "NSData+Base64.h"

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

Hope it helps

希望它能帮助

Swift 3 Version

斯威夫特3版本

It's pretty much the same

几乎是一样的

//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 Version

斯威夫特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


12  

You can decode a Base64 encoded string to NSData:

您可以解码一个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;
}

Example use of above method to get an image from the Base64 string:

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

-(void)imageFromBase64EncodedString{

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

    // do something with image
}

#3


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

#4


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;

#5


0  

This will work for ios7 and above.

这将适用于ios7和以上版本。

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

And use the code like this:

使用如下代码:

UIImage *img = [self decodeBase64ToImage:<ur base64 string>];

#6


0  

Swift 3 version

Use type Data instead of NSData

使用类型数据代替NSData

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