从UIView捕获UIImage不起作用

时间:2013-06-06 21:13:26

标签: iphone ios

我正在尝试从使用掩码图层的自定义视图中捕获图像  [view.layer renderInContext:UIGraphicsGetCurrentContext()];但它不起作用。图像的形状是矩形,但应该是自定义的。这是来自自定义UIView的代码,下面是来自捕获图像的代码。

@implementation ViewForAnn
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        CALayer *mask = [CALayer layer];
        mask.contents = (id)[[UIImage imageNamed:@"customPin"] CGImage];
        mask.frame = self.bounds;

        self.layer.mask = mask;
    }

    return self;
}
@end


UIGraphicsBeginImageContext(CGSizeMake(32.0, 37.0));
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
customAnnotationView.image = img;
UIGraphicsEndImageContext();

你能帮我把它弄好吗?

1 个答案:

答案 0 :(得分:0)

你可以试试这个。

UIGraphicsBeginImageContextWithOptions(yourView.frame.size, yourView.opaque, 0);
[yourView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imgView =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();