我试图通过淡入淡出和UIImageView
来实现类别以显示来自网址的UIActivityIndicatorView
,我在网上找到了一些内容并且我已经在最终版本中进行了编辑:
的UIImageView + AFNetworkingLoad.m
@implementation UIImageView (AFNetworkingLoad)
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage fadeInWithDuration:(CGFloat)duration withLoadIndicator:(UIActivityIndicatorView *)load_indicator {
[load_indicator startAnimating];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPShouldHandleCookies:NO];
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
__weak typeof (self) weakSelf = self;
[self setImageWithURLRequest:request placeholderImage:placeholderImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[load_indicator stopAnimating];
if (!request) // image was cached
[weakSelf setImage:image];
else
[UIView transitionWithView:weakSelf duration:duration options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[weakSelf setImage:image];
} completion:nil];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
[load_indicator stopAnimating];
}];
}
然后我以这种方式在自定义UITableViewCell
中使用它:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
...
[cell.img_view setImageWithURL:[NSURL URLWithString:my_url] placeholderImage:nil fadeInWithDuration:0.3 withLoadIndicator:cell.load_img];
...
}
一切正常,现在我的问题是,通过这个实现传递UIActivityIndicatorView的引用,我可以有一些内存泄漏吗?还是保留问题?显然我在我的iOS项目中使用ARC ......
答案 0 :(得分:0)
此类别不应导致内存泄漏。该方法将对象传递给复制块,因此临时保留该块,但是一旦图像加载或加载失败,就应该清除块,这会丢弃对象的保留计数。