繁体   English   中英

从UIView捕获UIImage不起作用

[英]Capture UIImage from UIView doesn't work

我正在尝试使用[view.layer renderInContext:UIGraphicsGetCurrentContext()];从具有蒙版层的自定义View捕获图像[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();

你能帮我使它工作吗?

你可以试试看

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM