ios - 从 UICollectionView 可见区域创建 UIImage

标签 ios uicollectionview uigraphicscontext

我有一个水平滚动的 Collection View 。我需要从 Collection View 的当前可见部分创建一个 UIImageView。

我通常使用以下方法:

+ (UIImageView *) imageCopyOfView:(UIView *)inputView
{
    UIGraphicsBeginImageContext(inputView.bounds.size);
    [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView *retView = [[UIImageView alloc] initWithImage:viewImage];
    return retView;
}

但它在滚动后不适用于 Collection View,因为它似乎正在获取已滚动到屏幕之外的 View 的一部分

最佳答案

代替

[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];

你应该使用

[inputView drawViewHierarchyInRect:inputView.frame afterScreenUpdates:YES];

关于ios - 从 UICollectionView 可见区域创建 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23551734/

相关文章:

ios - 无效的 IAP 产品标识符

ios - 子类化 UICollectionViewLayoutAttributes

ios - UICollectionView 的弹性 header

ios - 应用程序崩溃 setObject ForKey : object cannot be nil

IOS 绘制一个简单的图像结果使用 Xamarin C# 变得模糊

iphone - 如何在 iOS 编程中使用 pList

iphone - 如何在代码中识别 UITableViewCell?

ios - Animate UICollectionViewLayout header 大小会导致不必要的 Flash 交叉溶解

swift - 合并图像和文本在 swift 4 中创建另一个图像

ios - 防止 UICollectionViewCell 的一部分出现 `didSelect…`