1

I have following code for loading image from url in xml parsing endElement method :

food.image=strVal;
NSData *data=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:strVal]];
UIImage *image=[[UIImage alloc]initWithData:data];
food.myImage=image;

Although I am using this loaded images at the end of application,my application has to wait till all image get loaded. I supposed to use cache here but i am confused how to use the cache in this application. Is there any other way?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
V-Xtreme
  • 7,230
  • 9
  • 39
  • 79
  • 1
    I can really suggest that you have a look at [AFNetworking](https://github.com/AFNetworking/AFNetworking/) they hane a great extension on `UIImageView` to load and cache images. – rckoenes Jun 19 '12 at 10:15
  • try this: http://stackoverflow.com/questions/2149929/how-to-know-when-nsdatas-initwithcontentsofurl-has-finished-loading – sachin Jun 19 '12 at 10:54

1 Answers1

0

Try this code this will not create load on your main thread:-

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

dispatch_async(queue, ^{

    NSData *data=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:yourURL]];
    UIImage *image=[[UIImage alloc]initWithData:data];

    dispatch_sync(dispatch_get_main_queue(), ^{
        [yourImageView setImage:image];

    });
});
Leena
  • 2,678
  • 1
  • 30
  • 43