0

我试图通过使用 GCD 为 Collection 视图加载单元格来解决性能问题。我的单元格加载代码如下:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath{

UICollectionViewCell *slideCell =
[collectionView dequeueReusableCellWithReuseIdentifier:slideCellIdentifier
                                          forIndexPath:indexPath];

slideCell.backgroundColor = [UIColor lightGrayColor];
slideCell.selectedBackgroundView = [[UIView alloc]initWithFrame:slideCell.bounds];
slideCell.selectedBackgroundView.backgroundColor  = [UIColor redColor];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    @autoreleasepool {
        SlideHeader *slideHeader = [self.fetchedResultsController objectAtIndexPath:indexPath];
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        imageView.frame = CGRectMake(2, 2, slideCell.bounds.size.width-4, slideCell.bounds.size.height -4);

        if (slideHeader.frontCoverImage) {

            NSData *frontCoverImage = slideHeader.frontCoverImage;
            imageView.image = [UIImage imageWithData:frontCoverImage];

        }
        dispatch_async(dispatch_get_main_queue(), ^{



            NSArray *visibleCells = [self.collectionView visibleCells];
            if ([visibleCells containsObject:slideCell]) {


                [slideCell.contentView addSubview:imageView];



                if (slideHeader.slideTitle.length > 0) {


                    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(slideCell.bounds.origin.x, slideCell.bounds.size.height - 30, slideCell.bounds.size.width, 30)];
                    titleLabel.text = slideHeader.slideTitle;
                    titleLabel.backgroundColor = [UIColor lightTextColor];
                    titleLabel.font = [UIFont boldSystemFontOfSize:18];
                    titleLabel.textColor = [UIColor blackColor];
                    titleLabel.textAlignment = UITextAlignmentCenter;
                    titleLabel.userInteractionEnabled = NO;
                    [slideCell.contentView addSubview:titleLabel];
                }
            }
        });
    }
});


return slideCell;

}

我正在使用 ARC 并内置 6.1。我相信 arc 会为我释放 GCD 内存,但我最终遇到了一个问题:

以下是内存使用的屏幕截图:

在此处输入图像描述

当我继续上下滚动收藏视图时,内存上升了。我花了很长时间才释放内存,当内存使用率足够高时,这会导致程序崩溃。

我尝试像在我的代码中一样在 GCD 块中添加 autorealse,但这无助于更快地释放内存。

我能问我能做些什么来解决这个问题吗?谢谢你。

4

1 回答 1

0

我通过如下更改 GCD 块来解决问题:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    @autoreleasepool {
        NSData *frontCoverImage;

        if (slideHeader.frontCoverImage) {

            frontCoverImage = slideHeader.frontCoverImage;



        }
        dispatch_async(dispatch_get_main_queue(), ^{



            NSArray *visibleCells = [self.collectionView visibleCells];
            if ([visibleCells containsObject:slideCell]) {

                imageView.image = [UIImage imageWithData:frontCoverImage];

                [slideCell.contentView addSubview:imageView];



                if (slideHeader.slideTitle.length > 0) {


                    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(slideCell.bounds.origin.x, slideCell.bounds.size.height - 30, slideCell.bounds.size.width, 30)];
                    titleLabel.text = slideHeader.slideTitle;
                    titleLabel.backgroundColor = [UIColor lightTextColor];
                    titleLabel.font = [UIFont boldSystemFontOfSize:18];
                    titleLabel.textColor = [UIColor blackColor];
                    titleLabel.textAlignment = UITextAlignmentCenter;
                    titleLabel.userInteractionEnabled = NO;
                    [slideCell.contentView addSubview:titleLabel];
                }
            }
        });
    }
});
于 2013-07-27T19:24:27.650 回答