I am trying to load image from a remote server on the UIImageView in my application. Is it possible to load image from a remote server. I am using the following code
id path = #"http://upload.wikimedia.org/wikipedia/commons/c/c7/Sholay-Main_Male_Cast.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
I found this code sippnet on the net, but I dont understand how do I load this image on the ImageView.
Can anyone tell me how to add image on the UIImageView.
Thanx in advance...
you got almost all the way there... now just
imageView.image = img;
note that using dataWithContentsOfURL will block your user interface while the image loads, which will work if you're loading small images at startup, but is generally a bad idea; look into NSURLConnection.
Related
I'm developing an iOS application consisting of a static sqlite database, series of tableviews and tab based detail view where the first view loaded in the detailview is a swipable imageView which loads a series of images.
I've got it working with this code where it looks for the image locally but I'd like to have it load the image from a URL or display a default image if no internet connection is available.
The images are named in the database as (for the example) image.jpg and I'd like all of them to load from the same URL directory (for the example) http://www.someurl.com/images/
Thank you
- (UIImage *) imageAtIndex:(NSUInteger)index {
Image *currentImage = (Image *) [self.images objectAtIndex:index];
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#",[[currentImage filename] stringByDeletingPathExtension]] ofType:#"jpg"];
return [UIImage imageWithContentsOfFile:path];
}
The process for this is really very simple:
Try to download your image data using NSURLConnection
If successful, create a UIImage from the data
If either of the above fails, switch over to your placeholder/built-in image
Display the resulting image
I advise you have some sort of placeholder while the image is downloading, since that can take quite a while.
Don't bother with reachability; it's not 100% reliable, whereas actually trying the download is.
Avoid doing this on a background thread or queue. NSURLConnection is asynchronous out of the box to make this easier for you. There are a ton of third-party frameworks that try to simplify working with connections if you wish, though.
NSString *urlStr = [#"http://www.someurl.com/images/" stringByAppendingString:[currentImage filename]];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
Or something like that...
If the image is large or the internet connection is slow, the NSData initialization might take some time. Consider getting the images data in a background thread, or use some existing framework for that like SDWebImage
In my app I have implemented a method to download a image from a URL and display it in a TableView cell. The code is working fine but I also need to implement two more functions,
1) a UIActivityIndicator to show that the image is being downloaded.
2) Check whether the image is already downloaded. Download only if the cell is empty.
Here is my code.
-(void) downloadImage
{
NSURL *url = [NSURL URLWithString:#"http://cdn.iphonehacks.com/wp-content/uploads/2012/09/iphone5-front-back.jpg"];
UIImage *image = [[UIImage alloc] initWithData: [NSData dataWithContentsOfURL:url]];
[dCellImageView setImage:image];
[image release];
}
Thanks.
Use [NSData dataWithContentsOfURL:URL options:NSDataReadingMapped error:&error]. It is a synchronized method. So probably start activityIndicator before sending the message, and check error, then stop activityIndicator. What do you mean by 'cell is empty'? Do you store image in disk or just memory. If former, use NSFileManager to check if file exists.
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
It'll show an indicator on the left of the status bar. You can also use MBProgressHUD if you like.
And if you want to check whether the image has downloaded already before downloading, you need to store a key in your Core Data or Property List.
I am using following code to display image from server in UIImageView.
NSURL *url = [NSURL URLWithString:#"http://www.gettyicons.com/free-icons/125/miscellaneous/png/256/apple_256.png"];
NSData *imageData =[[NSData alloc] initWithContentsOfURL:url];
imgLargePicture.image = [[UIImage alloc] initWithData:imageData];
I can see the picture in Saffari but the image is not rendered in the iPhone application. I searched a lot on net and even in stackoverflow, everywhere this same code is given to display image from server. But in my case somehow its not working.
Can anyone please help me with this?
I use the same code of you and i get the image, so please check the NSData and make sure that imageView IBOutlet should be properly connected. Same code is running fine at my side i have check in xcode.
Just tried that code in my app and it worked. You are not adding that "imgLargePicture" (I assume it's a UIImageView) as your UIViewController's view subview or not giving it a proper frame.
Try this in your -viewDidLoad: and see if it works
UIImageView *imgLargePicture = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:imgLargePicture];
NSURL *url = [NSURL URLWithString:#"http://www.gettyicons.com/free-icons/125/miscellaneous/png/256/apple_256.png"];
NSData *imageData =[[NSData alloc] initWithContentsOfURL:url];
imgLargePicture.image = [[UIImage alloc] initWithData:imageData];
This was happening because of proxy server setup in the organization. It required me to provide user name & password to go through proxy. So this is fixed by implementing AuthenticationChallenge method where I passed the credentials.
I have make a one small image and in which one detail page is there.
and I in detail page I have one image tag which is in webview with html page.
It's work completely but image take a much time for loading and show in the screen.
So how can I reduce the time of loading for image. and this image is come form url.
imgBiteSpot.clipsToBounds=YES;
NSData *imageData = [[[NSData alloc]init]autorelease];
imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ObjBitSpot.StrImagePath]];
if(imageData==0)
{
imgBiteSpot.image=[UIImage imageNamed:#"img-not-found.gif"];
}
else {
UIImage *imgs = [[UIImage alloc] initWithData:imageData];
UIGraphicsBeginImageContext(CGSizeMake(88,88));
[imgs drawInRect:CGRectMake(0.0, 0.0, 88.0, 88.0)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imgBiteSpot.image=newImage;
[imgs release];
}
You could consider not downloading the image but downloading a one characters variable/flag from a web service. Depending on the answer from the web service you could load an image locally from the bundle? This could be faster.
Chris.
This is best example for loading image.
http://www.dimzzy.com/blog/2009/11/remote-image-for-iphone/
Its take time base on image size and internet speed.
But one thing you can do in your application.
You can download asynchronously that image and store it in temporary variable before you go in detail image view. When you go on that detail image you can load that downloaded image as per your requirement.
I'm loading images from url into webviews but that's showing me images which are cut down. I tried doing sizeToFit, but that shows a very small image cornered at left in my webview as the webage is large and image at its upper left corner.
EDIT:
This' how I'm loading images in webview. Here, expanded_photo is webview.
NSString *urlAddress = [NSString stringWithFormat:#"%#",photo_url];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[expanded_photo loadRequest:requestObj];
Whenever I try loading it through uiimageview using a uiimage like following, Here expanded_photo is imageview which I'm creating in a nib file:
UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photo_url]]];
expanded_photo.image = image1;
This takes a lot of time to load.
I want a solution which can load image from url in a small amount of time and the image is not cut.
Can anybody please help?
Thanx in advance.
Be aware that -[NSData dataWithContentsOfURL:] loads the data synchronously and thus blocks your main thread until the download is finished. When you load multiple images that way, all those loading operations are executed sequentially. This is not only slow but also your app is unresponsive during loading.
You might consider moving the actual load request in a background thread (using NSOperationQueue) or use NSURLConnection to asynchronously load the image.