Nuzhat Zari
Nuzhat Zari

Reputation: 3408

Using blocks to download image, freezing UI

In my app I am downloading image using blocks but it is freezing my UI. I have one network class which contains method to download image,

-(void)downloadImageWithCompletionHandler:^(NSData *aData, NSError *error)aBlock;

I am calling above method in my view controller to download image. So once the image is downloaded I am using NSData to show in image view. The network class method uses NSURLConnection methods to download the image.

   [[NSURLConnection alloc] initWithRequest:theURLRequest delegate:self];

Once the data download is complete I am calling completion handler block of the view controller.

But I am not sure why my UI is freezing? Can anyone help me find where I am doing wrong?

Thanks in advance!

Upvotes: 0

Views: 224

Answers (3)

Nuzhat Zari
Nuzhat Zari

Reputation: 3408

I figured out the problem. Problem was not in the block or using NSUrlConnection method, it is working properly. Problem was, I was saving data in file once I download it. This operation was happening on main thread which was blocking the UI.

Upvotes: 0

tounaobun
tounaobun

Reputation: 14857

You need to download the image in background thread to avoid freezing the UI thread.There is a simple demo to achieve this.

- (void)downloadImageWithCompletionHandler:(void(^)(NSData *aData, NSError *error))aBlock {

    NSURLRequest *theURLRequest = nil;  // assign your request here.
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    [NSURLConnection sendAsynchronousRequest:theURLRequest queue:mainQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        // UIThread.
        aBlock(data,connectionError);
    }];
}

how to call this method.

[self downloadImageWithCompletionHandler:^(NSData *aData, NSError *error) {
    // get UIImage.
    UIImage *image = [UIImage imageWithData:aData];
}];

Upvotes: 0

Varun Naharia
Varun Naharia

Reputation: 5420

- (void) setThumbnailUrlString:(NSString *)urlString
{
    NSString *url= [NSString stringWithFormat:@"%@",urlString];
    //Set up Request:
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:[NSURL URLWithString:url]];

    NSOperationQueue *queue=[[NSOperationQueue alloc] init];
    if ( queue == nil ){
        queue = [[NSOperationQueue alloc] init];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * resp, NSData     *data, NSError *error)
     {
         dispatch_async(dispatch_get_main_queue(),^
                        {
                            if ( error == nil && data )
                            {
                                UIImage *urlImage = [[UIImage alloc] initWithData:data];
                                _headImageView.image=urlImage;
                                _backgroundImageView.image=urlImage;
                            }
                        });
     }];
}

Upvotes: 1

Related Questions