2013-09-22 98 views
2

UIGraphicsBeginImageContextWithOptions没有捕捉阴影/梯度

我使用UIGraphicsBeginImageContextWithOptions的屏幕截图保存到一个UIImage的问题。虽然这起作用,但生成的UIImage缺少任何阴影和渐变。

我该如何得到它也截图层的东西?


TEST CODE表示问题


#import "ViewController.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation ViewController 

- (void)viewDidLoad { 
    //super 
    [super viewDidLoad]; 

    //background 
    self.view.backgroundColor = [UIColor whiteColor]; 

    //container 
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)]; 
    container.backgroundColor = [UIColor blackColor]; 
    [self.view addSubview:container]; 

    //circle 
    UIView *circle = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 10, 10)]; 
    circle.backgroundColor = [UIColor whiteColor]; 
    circle.layer.cornerRadius = 5; 
    circle.layer.shadowColor = [UIColor redColor].CGColor; 
    circle.layer.shadowOffset = CGSizeZero; 
    circle.layer.shadowOpacity = 1; 
    circle.layer.shadowRadius = 10; 
    circle.layer.shadowPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-10, -10, 30, 30)].CGPath; 
    [container addSubview:circle]; 

    //screenshot 
    UIGraphicsBeginImageContextWithOptions(container.bounds.size, container.opaque, 0.0); 
    [container.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    //image view 
    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(10, 568-200-20-10, 300, 200)]; 
    iv.image = screenshot; 
    [self.view addSubview:iv]; 
} 

@end 

SIMULATOR屏幕截图显示技术问题


顶部区域是原始视图。底部区域是带有屏幕截图的UIImageView。

Simulator Screenshot

+0

[[圈层] setNeedsDisplay]; –

+0

刚刚尝试setNeedsDisplay。为了安全起见,每隔一行调用一次。截图中仍然没有影子。 –

回答

0

在这一行,

UIGraphicsBeginImageContextWithOptions(container.bounds.size,container.opaque,0.0);

put NO instead of container.opaque 

使用此方法,

- (UIImage *)imageOfView:(UIView *)view 
{ 

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0); 
    } else { 
     UIGraphicsBeginImageContext(view.bounds.size); 
    } 

    [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return img; 
} 
+0

试过了,仍然没有捕捉到红色的部分 –