How to convert UIView to UIImage without backgroun

2019-02-21 11:49发布

I have UIView that contain a pin image and a label, as we know UIView is rectangle so if I convert UIView to UIImage,UIImage is also rectangle, I want make UIImage like the a pin image because if user click a background, UIImage's event will be called too.. I want to convert UIView to UIImage without it's background, how can it be?

I use this method to change UIView to UIImage

-(UIImage *) ChangeViewToImage : (UIView *) view{

UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return img;
}

Hmm.. example like this, I have a rectangle as a UIView and a triangle as a shape in UIView. and then I want to make this UIView become a UIImage, but I just want the triangle without background, so the image just a triangle.. how can I do It?

1条回答
Anthone
2楼-- · 2019-02-21 12:36

A UIImage is always rectangular. What you can play with is the opacity in your views. If you make everything except your pin image transparent, it will seem as if you just have an image of the pin.

Use:

view.backgroundColor = [UIColor clearColor];

to make your view's background transparent.

查看更多
登录 后发表回答