iPhone How to get image back from NSData - iphone

In my iPhone App I am storing image into NSData by
UIImage *image =imageView.image;
NSData *myData = UIImagePNGRepresentation(image);
But How to get back "image" from "NSData" and I can display it again into in to UIImageView.
Please Help and Suggest,
Thanks.

have you tried +[UIImage imageWithData:]?

UIImage *img=[UIImage imageWithData:data];
[yourImageView setImage:img];

Related

display an image from online into imageview of iphone and save in to database

I want to dispay an online image in imageview and save it to database
following code display only a particular image. I can also save that image to database.
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://tvaraj.files.wordpress.com/2012/03/sachin-tendulkar-when-young.jpg&w=231&h=2500&ei=SiKqT9-fKILprQfIudWCAQ&zoom=1"]]];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
img.image=[UIImage imageWithData:data1];
NSLog(#"image=%#",img.image);
imageToUse1= img.image;
[image release];
//i want to add this code. this is for
WebView *Wview=[[WebView alloc]init];
[self.navigationController pushViewController:Wview animated:YES];
My requirement is if I click any image that should display in the imageview.
How is it possible?
UIImage* myImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString: #"http://example.com/image.jpg"]]];
And if you want to use it on an image view just do the following:
// Let's assume the picture is an instantiated UIImageView
[picture setImage: myImage];
[myImage release];
Save image to Database
NSData *data1 = UIImagePNGRepresentation(myImage, 1.0); // save this nsdata to ur database.

convert NSData to UIImage in iphone

I want to convert nsdata into uiimage and display in imageview.
data is coming from database like this
NSData *imgdata = [[NSData alloc] initWithBytes:sqlite3_column_blob(cmp_sqlstmt,1)
length:sqlite3_column_bytes(cmp_sqlstmt,1)];
I tried this code
UIImage *image=[UIImage imageWithData:imgdata];
//set image to imageview
[img setImage:image];
but image is not showing. How do I solve this?
Here's a code example for JPEG, you can set the compression from 0 to 1 in decimals:
UIImage *yourimage = [UIImage imageNamed:#"YourImage.png];
NSData *imgData = UIImageJPEGRepresentation(yourImage, 1.0);
Edit:
There you go
NSData *yourData = somedata
UIImage *yourImage = [UIImage imageWithData:yourData];
try this
UIImage *imageObj = yourImage
NSData *imgData = UIImagePNGRepresentation(imageObj);
profileImage.image= [UIImage imageWithData:imgData];

Loading image from URL on iPhone

I have no idea why this isn't working.
NSData *data = [NSData dataWithContentsOfURL:place.iconData];
UIImage *image = [UIImage imageWithData:data];
[imageView setImage:image];
imageView is an IBOutlet hooked up through IB. place.iconData is http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png. For some reason, though, it won't load. Any help is much appreciated.
Try this:
NSURL *url = [NSURL URLWithString: #"http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
[imageView setImage:image];
//or imageView.Image=image;
Is your data being freed before it got assigned?
Maybe try manually allocating memory for the NSData:
NSData *data = [[NSData alloc] initWithContentsOfURL:place.iconData];
UIImage *image = [[UIImage alloc] initWithData:data];
[imageView setImage:image];
[data release];
[image release];
You need to use NSURL. Follow this url http://iosdevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html

Show image from url in uiimageview

I try to show a image in my iphone app but it doesn't show. I connect in IB and I check to show a local image, it workd good. I also check the url from image and it is ok. I use the next code:
NSURL *imageURL = [NSURL URLWithString: aVideo.urlThumbnail];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
imageView.image = image;
and
//Video.h
NSString *urlThumbnail;
and this is my code on the view.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tableView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"URLTHUMBNAIL: %#", aVideo.urlThumbnail);
NSURL *imageURL = [NSURL URLWithString: aVideo.urlThumbnail];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
imageView.image = image;
}
NSLog getts on console
URLTHUMBNAIL: http://147.83.74.180/thumbnails/56.jpg
The connections on IB are ok, cause I can display a local image using next code on the same IBAction
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"barret" ofType:#"png"]];
It can be very helpful to NSLog() yourself some notes about what's really in your variables. If you really do have everything hooked up right in IB, it could be that aVideo.urlThumbnail isn't getting set. I'd like to see that value in the console right before creating your NSURL with it.
First off, since NSData dataWithContentsOfURL: accesses the network, you should consider doing that in the background. Otherwise, your UI may become unresponsive.
You should consider adding some additional logging to check the value of imageData and image. Also, you can call NSData dataWithContentsOfURL:options:error: to see if that's the source of the failure.

How do I get a UIImage from an image file on my server?

How do I get a UIImage from a small image file on my server (can be .jpg or .png)?
Try this:
NSString* url = #"http://my.server.com/image.png";
NSData* data = [NSData dataWithContentsOfURL:url];
UIImage* image = [UIImage imageWithData:data];
See also:
[NSData dataWithContentsOfURL:options:error:]