2

I am using this code for displaying the image from URL to UIImageview

UIImageView *myview=[[UIImageView alloc]init];

myview.frame = CGRectMake(50, 50, 320, 480);

NSURL *imgURL=[[NSURL alloc]initWithString:@"http://soccerlens.com/files/2011/03/chelsea-1112-home.png"];

NSData *imgdata=[[NSData alloc]initWithContentsOfURL:imgURL];

UIImage *image=[[UIImage alloc]initWithData:imgdata];

myview.image=image;

[self.view addSubview:myview];

But the problem is that its taking too long time to display the image in imageview.

Please help me...

Is there any method to fast the process...

Master Stroke
  • 5,108
  • 2
  • 26
  • 57
Nithinbemitk
  • 2,710
  • 4
  • 24
  • 27
  • How do you expect the code to download data faster when it all depends on the strength of the network being used. – AppleDelegate Aug 29 '13 at 09:17
  • You could use the GCD as other answers suggested, but that will be equally as slow, it just won't freeze the app. – Lord Zsolt Aug 29 '13 at 09:20
  • It took me about 3 seconds to load the image. Are you loading this image alone at a time or are there more downloads running in paralell? Could you use some faster image download service? Is loading a smaller image file (smaller dimensions in pixels) an option? Can you switch to hgihly compressed JPEGs? You could use a framwork that enables some caching (which I don't think that NSData does). Is that an option? Meaning: Do you load the same image again and again? – Hermann Klecker Aug 29 '13 at 09:21
  • @HermannKlecker we are using different images for application... – Nithinbemitk Aug 29 '13 at 09:55
  • 1
    @HermannKlecker statements about how long a download takes for a certain resource when using mobile devices is *always* pointless. It may take up to 15 seconds alone to establish a connection. The right approach is to give the user a better experience, no matter how long that takes. – CouchDeveloper Aug 29 '13 at 10:53

3 Answers3

2

Instead of dispatch_async, Use SDWebImage for caching the images.

This is best I have seen...

The problem of dispatch_async is that if you lost focus from image, it will load again. However SDWebImage, Caches the image and it wont reload again.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • It's good but GDImageLoader is incredibly simple, solid - it's the best .. https://github.com/AndreyLunevich/DLImageLoader-iOS/tree/master/DLImageLoader No manual, no learning curve - one command. Totally fantastic. – Fattie Dec 11 '13 at 23:25
1
Use Dispatch queue to load image from URL.


dispatch_async(dispatch_get_main_queue(), ^{

  });

Or add a placeholder image till your image gets load from URL.
Aniket Kote
  • 541
  • 3
  • 9
  • you still have to worry about the cache and everything else. just use https://github.com/AndreyLunevich/DLImageLoader-iOS/tree/master/DLImageLoader – Fattie Dec 11 '13 at 23:25
1

The answers given to me on my own question Understanding the behaviour of [NSData dataWithContentsOfURL:URL] inside the GCD block does makes sense.So be sure that if you use [NSData dataWithContentsOfURL:URL] inside the GCD(as many developers do these days) is not a great idea to download the files/images.So i am leaning towards the below approach(you can either use NSOperationQueue).

Load your images using [NSURLConnection sendAsynchronousRequest:queue:completionHandler: then use NSCache to prevent downloading the same image again and again.

As suggested by many developers go for SDWebimage and it does include the above strategy to download the images files .You can load as many images you want and the same URL won't be downloaded several times as per the author of the code

EDIT:

Example on [NSURLConnection sendAsynchronousRequest:queue:completionHandler:

NSURL *url = [NSURL URLWithString:@"your_URL"];
NSURLRequest *myUrlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:myUrlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{

    if ([data length] > 0 && error == nil)
        //doSomething With The data

    else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
        //time out error

    else if (error != nil)
        //download error
}];
Community
  • 1
  • 1
Master Stroke
  • 5,108
  • 2
  • 26
  • 57