IOS集合查看从核心数据创建单元格图像并使用GCD

时间:2013-07-27 18:40:19

标签: ios multithreading memory-management automatic-ref-counting uicollectionview

我试图通过使用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内存释放但我最终遇到了一个问题:

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

enter image description here

当我继续向上和向下滚动集合视图时,妈妈上去了。 我花了很长时间才释放内存,这会导致程序在内存使用率足够高时崩溃。

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

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

1 个答案:

答案 0 :(得分: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];
                }
            }
        });
    }
});