NilPuig
4/5/2018 - 2:37 PM

[Download images Asynchronously (Fastest method)] #objectivec

[Download images Asynchronously (Fastest method)] #objectivec

// Call [self saveImages] to save an image and copy saveImage and downloadImageWithURL.

  

    - (void)saveImage{  
      
    NSDictionary *object = self.objects[i];  
      
    NSURL *aURL = [NSURL URLWithString:object[@"image"]];  
      
    [self downloadImageWithURL:aURL completionBlock:^(BOOL succeeded, UIImage *image) {  
    if (succeeded) {  
    cell.imageView.image = image;  
    }  
    }];  
    }
    
      
    
    - (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock  
    {  
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
    [NSURLConnection sendAsynchronousRequest:request  
    queue:[NSOperationQueue mainQueue]  
    completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {  
    if (!error) {  
    UIImage *image = [[UIImage alloc] initWithData:data];  
    completionBlock(YES,image);  
    }  
    else{  
    completionBlock(NO,nil);  
    }  
    }];  
    }