UIImage imageWithData:不在ARC下发布

时间:2013-06-06 12:31:38

标签: objective-c memory-leaks uiimageview automatic-ref-counting nsdata

您好我正在处理一个从Web加载图像以填充内容视图的应用程序。我使用“MGBox2”将视图加载到部分中,我在mgbox2示例中使用了“Photobox”类的修改版本。

我的问题是,当我从网络加载图像时,imageview对象保留在内存中,即使我解除/ removefromsuperview视图控制器和包含它们的视图。

当我查看内存分配时,我看到我有大量的“malloc 9.00kb”对象,其中保留计数为1,指向

UIImage* image = [UIImage imageWithData:weakData];

线。下面是我加载图像的全部代码并将其放入photobox:

我的代码看起来像一团糟,因为我不断尝试新事物,我知道我有一些问题,了解弧如何工作..试图将数据设置为__weak但你可以看到,仍然没有运气..

另请注意,如果图片网址中包含“publisher”字样,我会将其保存到文档目录中以便在磁盘上进行缓存。

- (void)loadPhotoFromURL:(NSString*)photoUrl; {

    bool hasLoaded = NO;
    for(id view in self.subviews){
        if([[view class] isSubclassOfClass:[UIImageView class]])
            hasLoaded = YES;
    }
    if(!hasLoaded){
        id fullPath = photoUrl;
        bool willCachePhoto = NO;
        if([photoUrl rangeOfString:@"publishers"].location!=NSNotFound){
            willCachePhoto = YES;
        }
        NSData* data;
        ASIHTTPRequest* request;
        if(willCachePhoto)
            data = [NewsUtil loadData:[fullPath md5]];
        if(!data){
            NSURL *url = [NSURL URLWithString:fullPath];
            request = [[ASIHTTPRequest alloc]initWithURL:url];
            [request startSynchronous];
            data = [request responseData];
            if(willCachePhoto){
                [NewsUtil saveData:data withName:[fullPath md5]];
            }
        }[request clearDelegatesAndCancel];
        // do UI stuff back in UI land
        dispatch_async(dispatch_get_main_queue(), ^{
            @autoreleasepool {
                // ditch the spinner
                UIActivityIndicatorView *spinner = self.subviews.lastObject;
                [spinner stopAnimating];
                [spinner removeFromSuperview];

                // failed to get the photo?
                if (!data) {
                    self.alpha = 0.3;
                    return;
                }
                __unsafe_unretained NSData* weakData = data;
                UIImage* image = [UIImage imageWithData:weakData];
                UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
                imageView.image = image;
                [self insertSubview:imageView atIndex:0];
                imageView.size = self.size;
                imageView.alpha = 0;
                imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth
                | UIViewAutoresizingFlexibleHeight;

                // fade the image in
                [UIView animateWithDuration:0.2 animations:^{
                    imageView.alpha = 1;
                }];
                for(id v in self.subviews){
                    if([[v class]isSubclassOfClass:[UILabel class]]){
                        UILabel* lbl = (UILabel*)v;
                        NSLog(@"lbl title: %@",lbl.text);
                        [self bringSubviewToFront:lbl];
                        break;
                    }
                }
            }//autorelease pool
        });
    }
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

好像我用changine imageWithData修复了我的问题到imageWithContentsOfFile构造函数。继承我新的photobox电话:

首先我将图像放入temproraycache(在少数情况下也进入永久缓存),然后我用文件名从缓存存储中调用图像......

        [wRequest setCompletionBlock:^{
            @autoreleasepool {
//                UIImage* image = [[UIImage alloc] initWithContentsOfFile:[[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:[NSURL URLWithString:photoUrl]]];;
                UIImage* image = [UIImage imageWithContentsOfFile:[[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:[NSURL URLWithString:photoUrl]]];
                dispatch_async(dispatch_get_main_queue(), ^{

                    UIActivityIndicatorView *spinner = self.subviews.lastObject;
                    [spinner stopAnimating];
                    [spinner removeFromSuperview];
                    if (!image) {
                        self.alpha = 0.3;
                        return;
                    }
                    UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
                    imageView.size = self.size;
                    imageView.alpha = 0;
                    imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth
                    | UIViewAutoresizingFlexibleHeight;
                    __weak UIImageView* wimageView = imageView;
                    [self insertSubview:wimageView atIndex:0];
                    [UIView animateWithDuration:0.3f animations:^{
                        wimageView.alpha = 1;
                    }];
                    imageView = nil;
                });
                image = nil;
            }
        }];

答案 1 :(得分:0)

您保留两次UIImageView实例。首先使用alloc创建它,然后将其插入另一个视图(insertSubview)。该父视图将保留它。

插入:     [imageView发布];

行后的

:[self insertSubview:imageView atIndex:0];

编辑:如果您使用ARC,那么我的答案无效。请忽略它。