iPhone - 扁平UIImageView和子视图到空白图像

问题描述:

我有一个UIImageView与无数个视图。其中一些视图有图层阴影或发光。该视图比设备屏幕稍大。iPhone - 扁平UIImageView和子视图到空白图像

这种观点基本上是一个包含许多对象(图像,按钮等)

现在我要弄平上以一个UIImage一切大的透明视图。然后,我这样做:

UIGraphicsBeginImageContext([viewWithZillionsOfObjects bounds].size); 
[[viewWithZillionsOfObjects layer] renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

结果等于完全透明的图像,但具有正确的大小。

我错过了什么?

感谢

+0

此代码:http://*.com/questions/3129352/need-to-capture-uiview-into-a-uiimage-including-all-subviews – AlexeyVMP 2013-02-18 15:19:34

+0

这个核心确实有效:HTTP:/ /*.com/questions/3129352/need-to-capture-uiview-into-a-uiimage-including-all-subviews – AlexeyVMP 2013-02-18 15:21:56

在苹果的示例代码,他们调整图形上下文的几何调用renderInContext:之前,该层的几何形状相匹配。

他们正在处理一个窗口,但它看起来像代码应该适用于任何视图,只需稍作更改。

我还没有尝试过构建这个,但这里是我尝试更改Apple的代码以在任何视图上工作。

- (UIImage*)imageFromView:(UIView *)view 
{ 
    // Create a graphics context with the target size 
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration 
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext 
    CGSize imageSize = [view bounds].size; 
    if (NULL != UIGraphicsBeginImageContextWithOptions) 
     UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); 
    else 
     UIGraphicsBeginImageContext(imageSize); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // -renderInContext: renders in the coordinate space of the layer, 
    // so we must first apply the layer's geometry to the graphics context 
    CGContextSaveGState(context); 
    // Center the context around the view's anchor point 
    CGContextTranslateCTM(context, [view center].x, [view center].y); 
    // Apply the view's transform about the anchor point 
    CGContextConcatCTM(context, [view transform]); 
    // Offset by the portion of the bounds left of and above the anchor point 
    CGContextTranslateCTM(context, 
          -[view bounds].size.width * [[view layer] anchorPoint].x, 
          -[view bounds].size.height * [[view layer] anchorPoint].y); 

    // Render the layer hierarchy to the current context 
    [[view layer] renderInContext:context]; 

    // Restore the context 
    CGContextRestoreGState(context); 

    // Retrieve the screenshot image 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return image; 
} 
+0

我需要的不完全是截图或渲染窗口,因为我需要渲染特别是一种观点,而不是全部。我期待图层属性包含在给定的UIView上可见的所有东西,但奇怪的是它没有。谢谢。 – SpaceDog 2010-10-06 14:42:06

+0

我认为你可以改变代码来操作你想要的视图。窗口位是偶然的。 – 2010-10-06 15:21:38

+0

我已经包含了在任何视图上操作的代码。 – 2010-10-06 15:26:22

这是一个通用版本(Swift 2.x),将UIViews数组压缩成单个UIImage。在你的情况下,只需传递一个由单个UIView组成的数组,它就可以工作。

// 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 
}