簡體   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