需要将UIView捕获成UIImage,包括所有子视图

发布于 2024-09-07 23:43:45 字数 178 浏览 2 评论 0原文

我需要将 UIView 及其所有子视图捕获到 UIImage 中。问题是视图的一部分位于屏幕之外,因此我无法使用屏幕捕获功能,并且当我尝试使用 UIGraphicsGetImageFromCurrentImageContext() 函数时,它似乎也无法捕获子视图。它应该捕获子视图而我只是做错了什么吗?如果没有,还有其他方法可以实现这一点吗?

I need to capture a UIView and all it's subviews into a UIImage. The problem is that part of the view is off screen, so I can't use the screen capture function, and when I try to use the UIGraphicsGetImageFromCurrentImageContext() function, it doesn't seem to capture the subviews as well. Should it be capturing the subviews and I'm just doing something wrong? If not, is there any other way of accomplishing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幽梦紫曦~ 2024-09-14 23:43:45

这是正确的方法:

+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

此方法是 UIImage 类的扩展方法,它还将负责使图像在任何未来的高分辨率设备上看起来都不错。

That's the right way to go:

+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

This method is an extension method for UIImage class, and it will also take care of making the image looks good on any future high-resolution devices.

终难愈 2024-09-14 23:43:45

你的意思

UIGraphicsBeginImageContext(view.bounds.size);
[view.layer drawInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

是不起作用?我很确定它应该...

Do you mean

UIGraphicsBeginImageContext(view.bounds.size);
[view.layer drawInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

doesn't work? I'm pretty sure it ought to...

喜爱纠缠 2024-09-14 23:43:45

斯威夫特 4+ 版本:

func getImage(from view:UIView) -> UIImage? {

    defer { UIGraphicsEndImageContext() }
    UIGraphicsBeginImageContextWithOptions(view.frame.size, true, UIScreen.main.scale)
    guard let context =  UIGraphicsGetCurrentContext() else { return nil }
    view.layer.render(in: context)
    return UIGraphicsGetImageFromCurrentImageContext()   

}

Swift 4+ Version:

func getImage(from view:UIView) -> UIImage? {

    defer { UIGraphicsEndImageContext() }
    UIGraphicsBeginImageContextWithOptions(view.frame.size, true, UIScreen.main.scale)
    guard let context =  UIGraphicsGetCurrentContext() else { return nil }
    view.layer.render(in: context)
    return UIGraphicsGetImageFromCurrentImageContext()   

}
你的笑 2024-09-14 23:43:45

这是一个 Swift 2.x 版本,如果您首先创建 UIViews 数组以进行扁平化,则该版本应该可以工作:

// Flattens <allViews> into single UIImage
func flattenViews(allViews: [UIView]) -> UIImage? {
    // Return nil if <allViews> empty
    if (allViews.isEmpty) {
        return nil
    }

    // If here, compose image out of views in <allViews>
    // Create graphics context
    UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, UIScreen.mainScreen().scale)
    let context = UIGraphicsGetCurrentContext()
    CGContextSetInterpolationQuality(context, CGInterpolationQuality.High)

    // Draw each view into context
    for curView in allViews {
        curView.drawViewHierarchyInRect(curView.frame, afterScreenUpdates: false)
    }

    // Extract image & end context
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    // Return image
    return image
}

Here's a Swift 2.x version that should work if you first create an array of the UIViews to get flattened:

// Flattens <allViews> into single UIImage
func flattenViews(allViews: [UIView]) -> UIImage? {
    // Return nil if <allViews> empty
    if (allViews.isEmpty) {
        return nil
    }

    // If here, compose image out of views in <allViews>
    // Create graphics context
    UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, UIScreen.mainScreen().scale)
    let context = UIGraphicsGetCurrentContext()
    CGContextSetInterpolationQuality(context, CGInterpolationQuality.High)

    // Draw each view into context
    for curView in allViews {
        curView.drawViewHierarchyInRect(curView.frame, afterScreenUpdates: false)
    }

    // Extract image & end context
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    // Return image
    return image
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文