将UIViews子视图展平到UIImage iPhone 3.0
我有一个UIView,它有几个UIImageView作为子视图。每个子视图都应用了不同的仿射变换。我想获取UIView的屏幕截图,将其捕获为UIImage或其他图像表示形式 我已经尝试过的方法,通过以下方式将层渲染到CGContext:将UIViews子视图展平到UIImage iPhone 3.0,iphone,cocoa-touch,cgcontext,Iphone,Cocoa Touch,Cgcontext,我有一个UIView,它有几个UIImageView作为子视图。每个子视图都应用了不同的仿射变换。我想获取UIView的屏幕截图,将其捕获为UIImage或其他图像表示形式 我已经尝试过的方法,通过以下方式将层渲染到CGContext: [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 不会保留子视图的定位或其他仿射变换 如果能踢对方向,我将不胜感激。试试这个: UIGraphicsBeginImageContext(sel
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
不会保留子视图的定位或其他仿射变换
如果能踢对方向,我将不胜感激。试试这个:
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
以下是Swift 2.x版本:
// This 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
}
//这将扁平化为单个UIImage
func平面视图(所有视图:[UIView])->UIImage?{
//如果为空,则返回nil
if(allViews.isEmpty){
归零
}
//如果在此处,请在中从视图中合成图像
//创建图形上下文
UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size,false,UIScreen.mainScreen().scale)
let context=UIGraphicsGetCurrentContext()
CGContextSetInterpolationQuality(上下文,CGInterpolationQuality.High)
//将每个视图绘制到上下文中
用于所有视图中的曲线视图{
CurveView.drawViewHierarchyInRect(CurveView.frame,后屏幕更新:false)
}
//提取图像和结束上下文
让image=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
//返回图像
返回图像
}
你去吧。我只需要将self.view.size更改为self.view.frame.size并使用renderInContext。我不知道我怎么会错过这个,我猜苹果文档中的关键短语是“在图层的坐标空间中渲染”。谢谢!从iOS4开始,您应该使用UIGraphicsBeginImageContextWithOptions(大小,否,0.0)如果希望它不透明,请使用YES
。如果您不需要alpha,则有助于提高性能。