-1

How can I download images into the app? Like I want to fetch images from my website and download it into the person's iphone app to be shown in the app? Basically the image won't be shown by url.

More specific:

How do I download the image to the app without using UIImage. I want to fetch the image and download it as the file name "anne.png" then reference it throughout the app using UIImage as anne.png. See - I want to download it first so that when someone visits the app a second time, they see the image, and see a default image in the meantime.. Thanks.?

Chris Hansen
  • 7,813
  • 15
  • 81
  • 165

2 Answers2

0

For single image you can use the following. Keep in mind this will block the UI till the image is fully downloaded.

[UIImage imageWithData:[NSData dataWithContentsOfURL:photoURL]];

To download the image without blocking the UI:

dispatch_queue_t downloadQueue = dispatch_queue_create(“image downloader”, NULL);

dispatch_async(downloadQueue, ^{
    [NSData dataWithContentsOfURL:photoURL];
    dispatch_async(dispatch_get_main_queue(), ^{
        UIImage *image = [UIImage imageWithData:imageData];
        // Code to show the image in the UI goes here
    });
});

To save the image to the phone camera roll you can use UIImageWriteToSavedPhotosAlbum.

To save the image in the app's directory. Use NSData's writeToFile:atomically:

To download several images from a website maybe this can help you:

What's the best way to download multiple images and display multiple UIImageView?

Community
  • 1
  • 1
Diego Allen
  • 4,623
  • 5
  • 30
  • 33
  • How do I download the image to the app without using UIImage. I want to fetch the image and download it as the file name "anne.png" then reference it throughout the app using UIImage as anne.png. See - I want to download it first so that when someone visits the app a second time, they see the image. Thanks.? – Chris Hansen Feb 08 '13 at 23:07
  • @BamBam As I suggested above, after you have the `NSData`, you can do `writeToFile` to save that to persistent storage. – Rob Feb 08 '13 at 23:21
0

http://mobiledevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html

URL to Remote Image

We start by creating a URL to the remote resource:

NSURL *url = [NSURL URLWithString: @"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];

Create UIImage from NSData

The next step is to build a UIImage using the data downloaded from the URL, which consists of an NSData object that holds the remote image contents:

 UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];

Putting it Together

Here’s how to wrap it all together, adding the remote image as a subview to an existing view by creating a UIImageView from the above UIImage:

   NSURL *url = [NSURL URLWithString:@"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 
  [self.view addSubview:[[UIImageView alloc] initWithImage:image]];
Garry
  • 4,996
  • 5
  • 32
  • 44
  • but it blocks? What can I do if it blocks? Will this download the image to the person's app storage? – Chris Hansen Feb 08 '13 at 22:00
  • Did you try this ? Is it blocked ??? – Garry Feb 08 '13 at 22:01
  • @BamBam You generally do steps one and two in a background queue and only do the updating of the UI, the "putting it together" in main queue. – Rob Feb 08 '13 at 22:46
  • I'd also suggest that you might do some optional caching to persistent storage. I'd suggest splitting your second step "create UIImage from NSData" into two steps, "download `NSData`" and "create `UIImage`". Then you can also save the NSData to persistent storage (e.g. `writeToFile`), so that the next time you need it, you've got it locally. – Rob Feb 08 '13 at 22:50