简体   繁体   English

如何在没有背景的情况下将UIView转换为UIImage?

[英]How to convert UIView to UIImage without background?

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? 我有UIView包含一个pin图像和一个标签,因为我们知道UIView是矩形所以如果我将UIView转换为UIImage,UIImage也是矩形,我想让UIImage像一个pin图像,因为如果用户点击背景,UIImage的事件将也被称为..我想在没有背景的情况下将UIView转换为UIImage,它怎么可能?

I use this method to change UIView to UIImage 我使用此方法将UIView更改为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. 嗯......像这样的例子,我有一个UIView的矩形和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? 然后我想让这个UIView成为一个UIImage,但我只想要没有背景的三角形,所以图像只是一个三角形..我怎么能这样做?

A UIImage is always rectangular. UIImage总是矩形的。 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. 使您的视图的背景透明。

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

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