3

I'm trying to get an image asynchronously from an url with AFNetworking 2.0

My problem is that neither success nor failure are called

I've checked my URL in a browser so the problem is not here

UIImageView *imgv = [[UIImageView alloc] init];
[imgv setImageWithURLRequest:myURL
            placeholderImage:nil
                     success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                        /*some code*/
                     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                        /*some code*/       
];

When I look deeper inside setImageWithUrlRequest:placeholderImage:success:failure

   __weak __typeof(self)weakSelf = self;
    self.af_imageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
    self.af_imageRequestOperation.responseSerializer = self.imageResponseSerializer;
    [self.af_imageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if ([[urlRequest URL] isEqual:[strongSelf.af_imageRequestOperation.request URL]]) {
            if (success) {
                success(urlRequest, operation.response, responseObject);
            } else if (responseObject) {
                strongSelf.image = responseObject;
            }

            if (operation == strongSelf.af_imageRequestOperation){
                    strongSelf.af_imageRequestOperation = nil;
            }
        }

        [[[strongSelf class] sharedImageCache] cacheImage:responseObject forRequest:urlRequest];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if ([[urlRequest URL] isEqual:[strongSelf.af_imageRequestOperation.request URL]]) {
            if (failure) {
                failure(urlRequest, operation.response, error);
            }

            if (operation == strongSelf.af_imageRequestOperation){
                    strongSelf.af_imageRequestOperation = nil;
            }
        }
    }];

My code goes trought [self.af_imageRequestOperation setCompletionBlockWithSuccess:^( AFHTTPRequestOperation *operation, id responseObject) { with success but i've noticed that __strong __typeof(weakSelf)strongSelf = weakSelf; are both nil

Any idea?

Titozzz
  • 189
  • 2
  • 6
  • 16
  • You need to hold onto a strong reference to `imgv` to prevent it being `dealloced`. What else do you do with it other than instantiating it and calling `setImageWithUrlRequest:placeholderImage:success:failure` on it? – Mike Pollard Sep 09 '14 at 10:14
  • When I was using afnetworking 1.xx I was doing this `[imgv setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { }] but i can't do this anymore in Afnetworking 2.0, maybe it's why it's not working anymore ? So i'm not doing anything else now – Titozzz Sep 09 '14 at 11:46
  • Maybe you should try adding the `imgv` to your view hierarchy and thus hold a strong reference to it so that it doesn't get dealloced... – Mike Pollard Sep 09 '14 at 11:53
  • I don't use the imageView for anything else than downloading the picture, as soon as I have the picture I save it to an other place. I'm trying to find a replacement for setDownloadProgressBlock in afnetworking 2.0 ... – Titozzz Sep 09 '14 at 12:01
  • Maybe look at `downloadTaskWithRequest:progress:destination:completionHandler:` – Mike Pollard Sep 09 '14 at 12:09
  • I finally used this thread : http://stackoverflow.com/questions/19501652/how-to-download-image-with-afnetworking-2-0 Thanks for helping me anyway :) – Titozzz Sep 09 '14 at 13:54

1 Answers1

4

Your imgv is never retained so it will be released after current scope so in your block weakSelf will be release when image is successfuly downloaded so thats why you are getting nil.

Zahid
  • 552
  • 3
  • 11
  • For me, I was initializing the image view in a method, and the method ended before the block returned, so obviously the image view had been deallocated. I fixed it by making the image view a class level variable – cph2117 Aug 31 '15 at 20:40