获取并保存UICollectionViewCell的屏幕截图

时间:2015-03-18 16:22:12

标签: objective-c uicollectionview screenshot uicollectionviewcell watchkit

如何截取我UICollectionView中所有单元格的屏幕截图并将其保存到磁盘(每个单元格在自己的文件中)? UICollectionViewCells非常复杂,我想在我的Watch App中显示它们。

1 个答案:

答案 0 :(得分:1)

我强烈建议不要为Apple Watch UI执行此操作。如果您确实想这样做,可以在每个- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates上致电UICollectionViewCell。代码看起来像(这是未经测试的代码,所以它可能有一些拼写错误,但你应该得到做什么的要点)。

for (int section = 0; section < collectionView.numberOfSections; section++)
{
    for (int row = 0; row < [collectionView numberOfItemsInSection:section]; row++)
    {
        NSIndexPath* path = [NSIndexPath indexPathForRow:row inSection:section];
        UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:path];
        //You may need to change the size here
        UIGraphicsBeginImageContext(cell.bounds.size);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [cell drawViewHierarchyInRect:cell.bounds afterScreenUpdates:YES];
        UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        //img has the image for the cell
    }
}
相关问题