0

How can I load images asynchronously? This is currently what I'm doing (borrowed from open source code for loading an image thumbnail):

- (UIImage*)thumbnailListView:(ThumbnailListView*)thumbnailListView
                 imageAtIndex:(NSInteger)index
{
    NSURL* url = [NSURL URLWithString:[recommendedArray objectAtIndex:index]] ;

//  [[cell grid_image] setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url]]];



NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
BOOL isImageLoaded = YES;
UIImage *bookImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", docPath, [[recommendedArray objectAtIndex:index] lastPathComponent]]];

if(bookImage == nil)
    isImageLoaded = NO;

if(!isImageLoaded){
    UIImage *thumbnailImage =[UIImage imageNamed:@"App-icon-144x144.png"];}
else{
    UIImage *thumbnailImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[recommendedArray objectAtIndex:index]]]]];
      return thumbnailImage;

}



}

I tried adding the following to the method, but it's producing an error for the NSURL:

dispatch_queue_t imageLoadQueue = dispatch_queue_create("com.g.load", NULL);

dispatch_async(imageLoadQueue, ^{
    //Wait for 5 seconds...
    usleep(1000000);

    NSArray *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];    

    for(int k=0; k < [images count];k++){
        NSLog(@"Loading %d Image", k);
        NSString *imgURL = [images objectAtIndex:k];
        NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgURL]];
        [imgData writeToFile:[NSString stringWithFormat:@"%@/%@", docPath, [imgURL lastPathComponent]] atomically:YES];
    }

    [[self collectionView] reloadData];
    NSLog(@"DONE WITH ALL IMAGES");
});

error

[NSURL length]: unrecognized selector sent to instance 0x12ea7240
2013-04-18 11:18:35.456 AssamKart[3084:14d03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL length]: unrecognized selector sent to instance 0x12ea7240'
Naveen
  • 1,251
  • 9
  • 20
  • 38

4 Answers4

0

Inside the method->

- (UIImage*)thumbnailListView:(ThumbnailListView*)thumbnailListView
                     imageAtIndex:(NSInteger)index  

Use this->

NSURLConnection *aNSURLConnection= [[NSURLConnection alloc]initWithRequest:<#(NSURLRequest *)#> delegate:<#(id)#> startImmediately:<#(BOOL)#>];

and the inside the delegate

- (void)connectionDidFinishLoading:(NSURLConnection *)connection; get the image data and implement your logic and here u shall call again the

- (UIImage*)thumbnailListView:(ThumbnailListView*)thumbnailListView
                     imageAtIndex:(NSInteger)index

method recursively till your recommendedArray is exhausted.

Ishank
  • 2,860
  • 32
  • 43
0

just do like this,

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    for (int k=0; k<[images count]; k++) {
    NSString *imgURL = [images objectAtIndex:k];
    NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:imgURL]];
    [data writeToFile:[NSString stringWithFormat:@"%@/%@", docPath, [imgURL lastPathComponent]] atomically:YES];

    }
});
iSpark
  • 952
  • 7
  • 18
  • instead of dispatch_async block in ur viewdidload – iSpark Apr 18 '13 at 06:39
  • and let me know the value of imgURL – iSpark Apr 18 '13 at 06:41
  • same exception ...and the execution not going into that dispatch its just exiting like condition false – Naveen Apr 18 '13 at 06:45
  • once comment all the code in your viewDidLoad: method and use only dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]]; }); – iSpark Apr 18 '13 at 06:49
  • and check whether data is coming or not? – iSpark Apr 18 '13 at 06:50
  • i think the probelm inside - (UIImage*)thumbnailListView:(ThumbnailListView*)thumbnailListView imageAtIndex:(NSInteger)index – Naveen Apr 18 '13 at 06:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28411/discussion-between-murali-and-naveen) – iSpark Apr 18 '13 at 07:04
0

you can use SDWebImage for downloading ur images from the internet and use the method:

[imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
MhammedMohie
  • 312
  • 2
  • 9
0

Use AsyncImageView.

https://github.com/nicklockwood/AsyncImageView

AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(Your Frame)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
NSURL *URL =  [NSURL URLWithString:[NSString stringWithFormat:URL]];
[AsyncImageLoader defaultCache];
[[AsyncImageLoader sharedLoader]loadImageWithURL:URL target:self success:@selector(successSelector) failure:@selector(failureSelector)];
Ramu Pasupuleti
  • 888
  • 2
  • 15
  • 35
  • You won't need to call this on a block manually...it will run asynchronously for you. – Reid Apr 18 '13 at 15:02