convert NSData to UIImage in iphone - 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];

Related

Not able to get images from a link to imageview

In my Iphone app.i'm not getting the images in imageview.Those images are coming from a link.
link is http://pointngo.testshell.net/Images/2013-01-05%2001-05-59_20110623033304.jpg
i'm using this code.
UIImage* myImage = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:str]]];
Try this :-
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
UIImage* myImage = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:str]]];
Hope it helps you
try this code...
NSURL *imgURL = [NSURL URLWithString:#"http://pointngo.testshell.net/Images/2013-01-05%2001-05-59_20110623033304.jpg"];
NSData *data = [[NSData alloc] initWithContentsOfURL:imgURL];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
yourImageView.image = tmpImage;
You should make sure the [NSData dataWithContentsOfURL: [NSURL URLWithString:str]] returns not nil . if nil you should use dataWithContentsOfURL:options:error:.
quote Apple Documentation :
Return Value
A data object containing the data from the location specified by aURL. Returns nil if the data object could not be created.
Discussion
If you need to know what was the reason for failure, use dataWithContentsOfURL:options:error:.
I've tried the following and it is working fine for me:
NSString *str = #"http://pointngo.testshell.net/Images/2013-01-05%2001-05-59_20110623033304.jpg";
//NSLog(#"Image data %#", [NSData dataWithContentsOfURL: [NSURL URLWithString:str]]);
UIImageView *imgv = [[[UIImageView alloc] initWithFrame: CGRectMake(50.0, 50.0, 100.0, 100.0)] autorelease];
imgv.image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:str]]];
imgv.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imgv];

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.

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

iPhone How to get image back from NSData

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

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