我有一个UIView
由其他子视图组成的。
这UIView
不是应用程序中任何其他视图的子视图。
我可以从中获得图像(UIImage
)UIView
吗?还是应该从显示的视图中引用它?
您不需要显示它。只需将该视图对象传递给此函数,您就会得到图像
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
func imageWithView (view: UIView)->UIImage
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
这是swift
版本