vendredi 31 juillet 2015

UIImageView+AFNetworking setImageWithURL causing memory leaks

My task is downloading captcha image to show in the UITableViewCell.
So I have a IBOutlet named captchaImageView and use UIImageView+AFNetworking to implement it.
The key codes as follows.

@property (weak, nonatomic) IBOutlet UIImageView *captchaImageView;
/**/
- (void)viewDidLoad {
    [super viewDidLoad];

    _captchaImageView.userInteractionEnabled = YES;
    [_captchaImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refreshCaptcha)]];
    [self refreshCaptcha];
}
- (void)refreshCaptcha {
    [_captchaImageView setHidden:YES];
    [_captchaImageIndicator startAnimating];

    __weak UIImageView *weakImageView = self.captchaImageView;
    [weakImageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:kCaptchaImageRequestUrl]
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                       timeoutInterval:kRefreshCaptchaTimeoutInterval]
                         placeholderImage:[UIImage imageNamed:@"RefreshCaptcha"]
                                  success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                      __strong UIImageView* strongImageView = weakImageView;
                                      [strongImageView setHidden:NO];
                                      [strongImageView setImage:image];
                                  }
                                  failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                  }];
}

However, it leads memory leaks. The below image is captured by Instruments leaks tool. enter image description here After rewrite the refreshCaptcha method, the leak also occured.

- (void)refreshCaptcha {
    [_captchaImageView setHidden:YES];

    __weak UIImageView *weakImageView = self.captchaImageView;
    [weakImageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:kCaptchaImageRequestUrl]
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                       timeoutInterval:kRefreshCaptchaTimeoutInterval]
                         placeholderImage:[UIImage imageNamed:@"RefreshCaptcha"]
                                  success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                  }
                                  failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                  }];
}

I can not understand that even the success and failure blocks are empty how the leaks can reach? Whether it is my incorrect use or UIImageView+AFNetworking has a bug about this?

Aucun commentaire:

Enregistrer un commentaire