简体   繁体   English

iPhone NSData到UIImage

[英]IPhone NSData to UIImage

my NSData object contains within byte of a photo. 我的NSData对象包含在照片的字节内。 I would like to convert this object to a UIImage. 我想将此对象转换为UIImage。 My problem is that he could not give a name all'oogetto UIImage. 我的问题是他无法给UIImage命名。 example myphoto.jpg 例如myphoto.jpg

this is my code: 这是我的代码:

NSString * bytestring =(NSString *)[bean image];

//contain byte of my photo
NSData * byte = [bytestring dataUsingEncoding:NSUTF8StringEncoding];


UIImage *image=[[UIImage alloc]initWithData:byte];


UIButton *bt=[[UIButton alloc]initWithFrame:CGRectMake(80,80,100,100)];

[bt setImage:image forState:UIControlStateNormal];
[self.boxView addSubview:bt];

the button does not display my photos. 该按钮不显示我的照片。 how can I do? 我能怎么做?

Since it appears that [bean image] is actually a base 64 encoding of the image's data, you need to use a method to convert the encoded data back into unencoded data. 由于似乎[bean image]实际上是图像数据的base 64编码,因此您需要使用一种方法将编码后的数据转换回未编码的数据。

There is no standard method for this but there are plenty you can find and use. 没有标准的方法,但是有很多可以找到和使用。 Your code then becomes something like this: 然后,您的代码将变成这样:

NSString *byteString = [bean image]; // no need for a cast if it's already an NSString
NSData *byte = // call some method to convert base 64 encoded string into an NSData object

Have a look at: 看一下:

convert base64 decoded NSData to NSString 将base64解码的NSData转换为NSString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM