UICollectionView imageview性能

时间:2013-12-08 17:00:54

标签: ios multithreading uiimageview uicollectionview uicollectionviewcell

我正在尝试创建一个集合视图,只显示我在设备上缓存的图像。这是我用来从缓存中获取它的代码:

-(void)loadImagesFromCache { //only runs on a background thread
    NSString *path = [NSString stringWithFormat:@"IMAGES%@", _eventID];
    NSMutableArray *photoInfoArray = [[PhotoUtil loadArrayForPath:path] mutableCopy];
    if (!photoInfoArray || photoInfoArray.count == 0) {
        return;
    }
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:photoInfoArray.count];
    for (NSDictionary *photoInfoDict in photoInfoArray) {
        PhotoInfo *photoInfo = [[PhotoInfo alloc] initWithDictionary:photoInfoDict];
        UIImage *image = [PhotoUtil getImageForPath:[photoInfo photoPath]];
        [images addObject:image];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        _allPhotos = images;
        [_collectionView reloadData];
        [_refreshControl endRefreshing];
    });
}

这是indexPath方法的单元格:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"PhotoCell";

    PhotoCell *cell = (PhotoCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell.imageView setBackgroundColor:[PhotoStyleSheet getBlueColor]];
    cell.imageView.layer.borderColor = [[PhotoStyleSheet getBlueColor] CGColor];
    cell.imageView.layer.borderWidth = 3.0f;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
    dispatch_async(queue, ^{
        UIImage *image = _allPhotos[indexPath.row];
        dispatch_async(dispatch_get_main_queue(), ^{
            [cell.imageView setImage:image];
        });
    });
    return cell;
}

每当图像首次加载时,不仅订单混乱了一会儿(它最终确实正确),但是当图像视图变为蓝色并且图像加载时,集合视图不可滚动。我显然在主线程上执行处理器密集型操作,我认为它在indexPath方法的单元格中,但我正在做的事情没有用。帮助

0 个答案:

没有答案