Wednesday, 2 July 2014

Load images Asynchrously- objective c

- (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);
}
}];
}

//// call the function where u want to display image

[self downloadImageWithURL:[img_url objectAtIndex:i] completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
// change the image in the cell
if(image==NULL)
{

}
else{

[array addobject:image];

}

}

}];

No comments:

Post a Comment