Loading image from URL on iPhone - 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

Related

Cannot Fetch UIImage from URL

i am trying to fetch an image from URL, but i am not able to do it.
can any one let me know what went wrong
find the code below for your reference.
NSString *ImageURL = #"http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/{B690E93E-F7C9-48AF-B72A-BFF944FA6D4A}_104_9169.JPG";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
img.image = [UIImage imageWithData:imageData];
where img is a UIImageView
i just create DEMO of Your ImageURL you just Need to put Code Bellow:-
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *ImageURL = #"http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/{B690E93E-F7C9-48AF-B72A-BFF944FA6D4A}_104_9169.JPG";
ImageURL =[ImageURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(#"img url ==%#",ImageURL);
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:ImageURL]];
UIImage *MYimage = [UIImage imageWithData:data];
NSLog(#"==%#",MYimage);
[imgview setImage:MYimage];
}
Download the Demo i just created of your image URL:-
http://www.sendspace.com/file/m966ae
Thank you :)
Try this.......
NSString *ImageURL = #"http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/{B690E93E-F7C9-48AF-B72A-BFF944FA6D4A}_104_9169.JPG";
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:ImageURL]];
UIImage *IMG = [UIImage imageWithData:data];
img.image = IMG;
This is because you have "{" and "}" in your imageURL so you are not able to get your image, so just replace this braces.
-(NSString*)replace:(NSString*)string
{
NSString *str = [[string stringByReplacingOccurrencesOfString:#"{" withString:#"%7B"] stringByReplacingOccurrencesOfString:#"}" withString:#"%7D"];
return str;
}
You have to create your url object in the following way :
NSURL *url = [NSURL URLWithString:[ImageURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
then use that url to fetch data. Hope it will work for you.
there are multiple ways to get Image from Url. you can use anyone. but the fastest one that i have tried is
-(UIImage*)getImageFromURLwithUrl:(NSString*)imgURLStr
{
NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imgURLStr]];
NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nil error:nil];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}

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

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

UIImage from URL difficulty

The following code works to load a UIImage from a URL. However, when I try to load an image from Craigslist, the image does not work. I have verified the craigslist imagePath is working.
NSURL *imageURL = [NSURL URLWithString:#"http://images.free-extras.com/pics/c/car-530.jpg"];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data] ;
self.imageView.image = img;
I have tried this
NSURL *imageURL = [NSURL URLWithString:#"http://images.craigslist.org/3m23pb3l85T65R05S4b8462d2236c156a1ce3.jpg"];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data] ;
self.imageView.image = img;
and even this
NSString * urlString = [#"http://images.craigslist.org/3m23pb3l85T65R05S4b8462d2236c156a1ce3.jpg" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL * imageURL = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data] ;
self.imageView.image = img;
Any help is much appreciated. My Error message reads:
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)
have you checked if the data and the probably evolving image structure are not nil?
Normally this should not happen, but it might be valuable to point out what went wrong.
The loading code looks good to me - I use something similar.
you can try
imageView.image = img;
instead of
self.imageView.image = img;

how can i display image from url?

I have a string variable tmpImgURLStr which contains URL like www.abc.com/img.png. i want to display that image in my imageView for that i have use some code but it's not working which is given below:
NSLog(#"Img URL === %#",tmpImgURLStr);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",tmpImgURLStr]]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[logoImg setImage:myimage];
As far as I can tell from your url - you have pdf, not an image. Usually WebViews are used for displaying this sort of data.
Update
Your NSData initiation is kinda too long. You can actually initiate a URL without supplying formatted string:
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:tmpImgURLStr]];
Also I've noticed that your URL is without protocol. You may want to try adding http:// or https:// to it and then see what happens. And just in case check if your logoImg is actually wired to the NSImageView in your NIB.
Try this
NSURL *imageurl = [NSURL URLWithString:#"http://www.chakrainteractive.com/mob/ImageUpoad/pic2-2.png"];
NSData *imagedata = [[NSData alloc]initWithContentsOfURL:imageurl];
UIImage *image = [UIImage imageWithData: imagedata];
[logoImg setImage: image];
Try this code instead of yours .. May be it will work..
logoImg=[[IUImageView alloc]initWithFrame:CGRectMake(10,10,300,460)];
NSLog(#"Img URL === %#",tmpImgURLStr);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:tmpImgURLStr]]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[logoImg setImage:myimage];
-Happy coding....