开发者

how to convert nsdata to image?

I am developing an application in which I take NSData and convert it into image an display it in the image view. Now i want to save this data and display it again in UIImageView. I used NSUserDefaults to save the NSData and again retrieve this data and convert into image and display it in image view.But i am not getting the image back.my code is as follows:

//for开发者_运维问答 saving  nsdata using nsuserdefaults method:-
NSUserDefaults *defsMVC=[NSUserDefaults standardUserDefaults];
[defsMVC setObject:stateMVC forKey:@"MapImageObject"];

//for retrieving nsdata from nsuserdefaults and display it in imageview
NSUserDefaults *defsMVC=[NSUserDefaults standardUserDefaults];

NSData *stringmapdata=[defsMVC dataForKey:@"MapImageObject"];

UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 6, 320,415)];
UIImage *image = [UIImage imageWithData:stringmapdata];
[imageView setImage:image];
[image release];
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
[imageView release];


I don't think you should be storing the data in the user Defaults, try storing it in a file like "yourimage.png", then you can simply do [[NSImage alloc] initWithConentsOfFile:path];

Also try using NSArchiver before saving it in the user defaults:

NSImage *image = [NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"someKey"]];

And for archiving it to the user defaults:

[[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:image] forKey:@"someKey"];

Gotta love Cocoa =)


My image is stored in 6th column in a form of NSData, first I have to get that data into NSData object. Than convert that NSData into image.

data = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 6)
                              length:sqlite3_column_bytes(statement, 6)];
img = [UIImage imageWithData:data];
imgdata = [[NSMutableArray alloc]initWithObjects:img, nil];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜