![](/img/trans.png)
[英](iphone) uiimage, view, subviews memory management
[英]Flatten UIViews Subviews to UIImage iPhone 3.0
我有一個UIView,它有幾個UIImageViews作為子視圖。 這些子視圖中的每一個都應用了不同的仿射變換。 我想采取相當於我的UIView的屏幕截圖,將其捕獲為UIImage或其他圖像表示。
我已經嘗試過的方法,將圖層渲染為CGContext:
[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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.