如何创建一组uiimages的副本?

时间:2012-06-12 02:03:49

标签: iphone objective-c ios copy nscache

所以这可能有点难以解释,但我会尽我所能。此问题可能需要几个步骤才能解决。

此代码显示在tableviewcell中,因此每次单元格出现在屏幕上时都会运行:

[cell.cellContent.thumbnails removeAllObjects];

if ([self.cellThumbnailCache objectForKey:[NSNumber numberWithInt:indexPath.row]]) {
    cell.cellContent.thumbnails = [self.cellThumbnailCache objectForKey:[NSNumber numberWithInt:indexPath.row]];
    [cell.cellContent setNeedsDisplay];
}
else {

    //First it removes all the existing UIImages from the cell.cellContent.thumbnails mutable array.
    int i = 0;

    while (i < numberOfThumbnailsToDraw) {
        Media *media = [entryNewMoc.media objectAtIndex:i];
        UIImage *image = [media getThumbnail];

        [newMoc save:nil];

        [cell.cellContent.thumbnails addObject:image];
        i++;
    }

    //Then it goes through getting UIImages and adding them to the array.
    [self.cellThumbnailCache setObject:cell.cellContent.thumbnails forKey:[NSNumber numberWithInt:indexPath.row]];
    [cell.cellContent setNeedsDisplay];

    //Then it all gets drawn in setNeedsDisplay.
}        

问题是,当它回来取消所有对象时,它似乎从记录中删除了实际的UIImages,因为thumbnailsCache上的NSLog显示该条目为空。这是执行它的removeAllObjects行,但如果保留它,那么它只是不断向单元格添加更多媒体。

1 个答案:

答案 0 :(得分:2)

尝试指定简单副本,例如ie。:

[self.cellThumbnailCache setObject:[NSSet setWithSet:cell.cellContent.thumbnails] ....