0

我正在尝试从使用蒙版层的自定义视图中捕获图像, [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();

你能帮我让它工作吗?

4

1 回答 1

0

你可以试试这个。

UIGraphicsBeginImageContextWithOptions(yourView.frame.size, yourView.opaque, 0);
[yourView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imgView =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
于 2013-06-07T07:00:25.957 回答