xcode 如何在没有背景的情况下将 UIView 转换为 UIImage?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6895090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert UIView to UIImage without background?
提问by user4951
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,因为我们知道 UIView 是矩形,所以如果我将 UIView 转换为 UIImage,UIImage 也是矩形,我想让 UIImage 像图钉图像一样,因为如果用户单击背景,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. 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 和一个三角形作为 UIView 中的形状。然后我想让这个UIView变成一个UIImage,但我只想要没有背景的三角形,所以图像只是一个三角形..我该怎么做?
采纳答案by adriaan
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.
UIImage 始终是矩形的。您可以使用的是视图中的不透明度。如果您将大头针图像以外的所有内容都设置为透明,则看起来好像您只有大头针的图像。
Use:
用:
view.backgroundColor = [UIColor clearColor];
to make your view's background transparent.
使您的视图背景透明。