2

我有一个UIView由其他子视图组成的。

UIView不是应用程序中任何其他视图的子视图。

我可以从中获得图像(UIImageUIView吗?还是应该从显示的视图中引用它?

4

2 回答 2

2

您不需要显示它。只需将该视图对象传递给此函数,您就会得到图像

+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;
}
于 2012-12-21T09:11:33.123 回答
0
func imageWithView (view: UIView)->UIImage
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

这是swift版本

于 2014-09-20T08:34:57.650 回答