Downloading images is a very common task in both iOS app and Android app. In Android app, there are several ways to download image by url. It is the same in iOS. Here I will demonstrate two ways to show you how to download images from internet in iOS app. Depending on your real project, you can choose one of the solutions.
The Simplest Way to Download Image in iOS
In most of the case, we can use the simplest way to download images by URL in iOS. You only need three steps in this approach.
NSString *urlStr = @"http://newevolutiondesigns.com/images/freebies/cat-wallpaper-2.jpg"; NSURL *url = [NSURL URLWithString:urlStr]; NSData *imageData = [NSData dataWithContentsOfURL:url]; image = [UIImage imageWithData:imageData]; return image;
This solution will meet most of the needs. The only problem is the downloading task is executed on the main UI thread. If the image size is big, it may take a long time to download. Therefore, the main UI thread will be blocked when you are downloading the image in this way. All UI will be frozen in this progress. If you need a way to download image and keep the UI alive at the same time, you can check the following way.
Download Image by URL in iOS
In this approach, we will use NSURLConnection to download the images. Actually, NSURLConnection is the standard way to get any type of data from internet. The advantage is it will create a new thread to process the downloading task (I guess) which will not block the main UI thread. We can consider NSURLConnection will do all download job in the backend. After the download task finishes or fails, the NSURLConnection will notify the delegate.
In our example, we will use NSURLConnectionDataDelegate protocol in our ViewController. In the ViewController. We will implement following methods. For more information, you can check another article: HTTP Network Programming in IOS
- – (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- – (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- – (void) connectionDidFinishLoading:(NSURLConnection *)connection
- – (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
First, let’s check the head file.
@interface ViewController : UIViewController <NSURLConnectionDataDelegate> { NSString *imageURL; NSURLConnection *urlConnect; BOOL isLoad; NSMutableData *downloadData; }
The imageURL is the image url which we will download the image from. Later, we will use urlConnect to download the image by URL. We will save all data downloaded by urlConnect in downloadData. Now, let’s see the example source code:
- (NSURLConnection *)loadImageWithNSUrl { NSString *imageURL = @"http://newevolutiondesigns.com/images/freebies/cat-wallpaper-2.jpg"; NSURL *url = [NSURL URLWithString: imageURL]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; urlConnect = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES]; isLoad = YES; }
The following example source code is the protocol method.
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { downloadData = [[NSMutableData alloc] init]; [downloadData setLength:0]; } - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [downloadData appendData:data]; } - (void) connectionDidFinishLoading:(NSURLConnection *)connection { UIImage *image = [UIImage imageWithData:downloadData]; [imageView setImage:image]; } - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"Image Load Error: %@", [error localizedDescription]); }
Get Full Source Code under $1.99
If you have any problems to implement downloading image in IOS, you can get my whole example source code at $1.99. You have the full permission to use the example source code in any projects. If you want to purchase the source code, please click above button and make the payment. After successfully making the payment, the downloading link will show in the same window.