Iphone 从UIView中捕获UIImage';行不通

Iphone 从UIView中捕获UIImage';行不通,iphone,ios,Iphone,Ios,我正在尝试从自定义视图中捕获图像,该视图使用 [view.layer renderContext:UIGraphicsGetCurrentContext()]但它不起作用。图像的形状为矩形,但应为自定义形状。这里是来自自定义UIView的代码,下面是来自capture image的代码 @implementation ViewForAnn - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame];

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

@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();
你能帮我把它弄好吗?

你可以试试这个

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

这是完美的工作。我能知道您在使用此代码时面临的问题是什么吗?