Search code examples
iosxcodeuitableviewafnetworking-2

UIImageView+Afnetworking doesn't Work Properly


I Have copied the contents of file from here and created a UIImageView+AFNetworking.h and imported in my implementation file

Now When I Write the following code i get this error but when i remove the block of code then everything works fine.

I want to display image in a custom table cell.The url of image i am grabbing Through JSON

NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[dictArray objectForKey:@"image"]]];
[cell.thumbnailImageView setImageWithURLRequest:imageRequest placeholderImage:nil
                               success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
                                   NSLog(@"success");
                                   cell.thumbnailImageView.image = image;
                                   cell.thumbnailImageView.contentMode = UIViewContentModeScaleAspectFit;
                                   cell.thumbnailImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
                                   [cell setNeedsLayout];// To update the cell {if not using this, your image is not showing over cell.}
                               }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                                   NSLog(@"Failure");}];

Here is the screen shot of the error

It Crashes after loading enter image description here


Solution

  • Instead of using afnetworking You can just use dispatch method

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
          NSString *url = [indexDic objectForKey:@"image"];
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
        UIImage *image = [UIImage imageWithData:imageData];
        dispatch_async(dispatch_get_main_queue(), ^{
    
            cellImg.image = image;
    
        });
    });
    
    return cell;
    
     }