【问题标题】:Create UIImage from UICollectionView Visible Area从 UICollectionView 可见区域创建 UIImage
【发布时间】:2023-03-09 10:09:01
【问题描述】:

我有一个水平滚动的集合视图。我需要从集合视图的当前可见部分创建一个 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 滚动后无法使用,因为它似乎正在获取已从屏幕上滚动的视图的一部分

【问题讨论】:

    标签: ios uicollectionview uigraphicscontext


    【解决方案1】:

    代替

    [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
    

    你应该使用

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

    【讨论】:

    • 嗯,它是不同的,但不完全一样。但我在动画中使用捕获的图像可能会在某处搞砸。继续看它。这可能已经得到了它,但我可能只是有其他导致问题的东西。谢谢回复。我会继续努力的。
    • 经过一番修补后,我发现问题是我调用它的方法中的框架有问题。您的解决方案正是我所需要的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2016-02-12
    • 1970-01-01
    • 2014-12-28
    • 2011-08-28
    相关资源
    最近更新 更多