创建UIView图像时出现问题

时间:2010-07-01 11:41:50

标签: iphone uiview uiimage

我想创建UIWebView的UIImage然后我想绘制该图像。

这是我的代码:

    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1004)];
 tempView.backgroundColor = [UIColor blueColor];

 UIGraphicsBeginImageContext(tempView.bounds.size);
    [tempView drawRect:tempView.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); 
 CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
 CGContextDrawImage(ctx, imageRect, [image CGImage]);

但它没有绘制除空白页面以外的任何内容。我们不能像那样创建UIImage,或者我在代码中的某处做错了什么。

感谢名单

1 个答案:

答案 0 :(得分:4)

下一个代码应该有效:

UIGraphicsBeginImageContext(tempView.bounds.size);
[tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
相关问题