سؤال

I'm writing a gallery with images which can be loaded by url with AFNetworking.

In Init method of the ImageView object I call a function that send a request. Here:

- (void) loadWithUrl:(NSURL *)url
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:TimeOut];
    [request setHTTPShouldHandleCookies:NO];
    [request setHTTPShouldUsePipelining:YES];

    __weak AOWImageView *safeSelf = self;

    m_operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
                                                       imageProcessingBlock:nil
                                                                    success:^
                   (NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
                   {
                       [safeSelf setImage:image];
                   }
                                                                    failure:^
                   (NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
                   {
                       [safeSelf setNoImageLabelOpaque];
                   }];

    [m_operation start];
}

If the ImageView is outside the visible part of the screen - (void) dealloc is called. I cancel operation loading image in this method so: [m_operation cancel];. I guess that the operations are not canceled because the memory is increasing and isn't released.

enter image description here

I think that there is retain cycle. I want to understand how to write it right. Thanks.

هل كانت مفيدة؟

المحلول

I don't see a retain cycle in the code snippet you posted, so if you are leaking memory, it may be due to another part of your app's code...

Regarding "best practices" for AFNetworking and loading images-

AFNetworking has a built in category on UIImageView that has convenience methods for setting an an image view's image via a URL.

See UIImageView+AFNetworking, specifically setImageWithURL: method and related. This also has the advantage of keeping a cache (so you don't have to fetch images again if requested multiple times), which AFAIK, doesn't appear to done by AFImageRequestOperation.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top