iPhone-将UIImageView和子视图展平为图像=空白图像

iPhone-将UIImageView和子视图展平为图像=空白图像,iphone,Iphone,我有一个拥有无数视图的UIImageView。其中一些视图具有层阴影或光晕。此视图略大于设备屏幕 该视图基本上是一个大的透明视图,包含许多对象(图像、按钮等) 现在我想将视图上的所有内容展平为UIImage。那么,我会: UIGraphicsBeginImageContext( [viewWithZillionsOfObjects bounds].size ); [[viewWithZillionsOfObjects layer] renderInContext:UIGraphicsGetCur

我有一个拥有无数视图的UIImageView。其中一些视图具有层阴影或光晕。此视图略大于设备屏幕

该视图基本上是一个大的透明视图,包含许多对象(图像、按钮等)

现在我想将视图上的所有内容展平为UIImage。那么,我会:

UIGraphicsBeginImageContext( [viewWithZillionsOfObjects bounds].size );
[[viewWithZillionsOfObjects layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
结果相当于一个完全透明的图像,但大小正确

我错过什么了吗


感谢

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

他们处理的是一个窗口,但看起来代码应该与任何视图一起工作,只需做一些小的更改

我还没有尝试过构建它,但我尝试着改变苹果的代码,使其在任何视图上都能工作

- (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;
}
下面是一个通用版本(Swift 2.x),用于将UIView数组展平为单个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
}
//展平为单个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()
//返回图像
返回图像
}

我需要的不是截图。。。或者渲染窗口,因为我需要专门渲染一个视图中的内容,而不是所有视图中的内容。我本来希望layer属性包含给定UIView上可见的所有内容,但奇怪的是它没有。谢谢。我想你可以把代码改成只在你想要的视图上操作。窗口位是偶然的。我可以做些什么来确保隐藏部分也包括在内?当前,它在视图边界上被切断。注意:我有一个TableView这个代码不关心子视图-它不适用于UIView。仍然得到没有子视图的空图像…此代码有效:此核心确实有效: