Powered By Blogger

Monday, January 27, 2014

Asynchronous image downloading with dispatch_async queue

Hi,

There's no need to implement any framework for LazyImage loading as of now. You can do it simply by writing few lines of code below.. Just take a look..


Example : 

UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]init];
UIImageView * userImage = [[UIImageView alloc]init];

        [activity startAnimating];
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^(void) {
            //  You may want to cache this explicitly instead of reloading every time.
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
            UIImage* image = [[UIImage alloc] initWithData:imageData];
            dispatch_async(dispatch_get_main_queue(), ^{
                // Capture the indexPath variable, not the cell variable, and use that
               userImage.image = image;
                [activity stopAnimating];
            });
       });


Regards,
Nilesh M. Prajapati

No comments:

Post a Comment