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

jam*_*mur 14 iphone uiview uiimage

我需要将UIView及其所有子视图捕获到UIImage中.问题是部分视图在屏幕外,所以我无法使用屏幕捕获功能,当我尝试使用UIGraphicsGetImageFromCurrentImageContext()函数时,它似乎也不会捕获子视图.它应该捕获子视图,我只是做错了吗?如果没有,还有其他方法可以实现吗?

Avi*_*ron 28

这是正确的方法:

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

这种方法是UIImage类的扩展方法,它还可以在任何未来的高分辨率设备上使图像看起来很好.


tc.*_*tc. 2

你的意思是

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

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