我正在尝试从使用蒙版层的自定义视图中捕获图像,
[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();
你能帮我让它工作吗?