5

我有一些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'

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

4

6 回答 6

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
}
于 2013-05-23T04:03:29.553 回答
10

NSData Base64 库文件将为您提供帮助。

#import "NSData+Base64.h"

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

希望能帮助到你

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

斯威夫特 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!)
于 2013-05-23T03:28:27.057 回答
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];
于 2014-05-13T10:03:13.933 回答
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;
于 2015-01-27T10:23:31.427 回答
0

这适用于 ios7 及更高版本。

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

并使用这样的代码:

UIImage *img = [self decodeBase64ToImage:<ur base64 string>];
于 2015-09-04T09:57:17.120 回答
0

斯威夫特 3 版本

使用类型 Data 而不是 NSData

let data = Data(base64Encoded: imageBase64String, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)
let image = UIImage(data:data!)
于 2017-02-01T09:24:16.087 回答