如何在没有背景的情况下将UIView转换为UIImage? [英] How to convert UIView to UIImage without background?

查看:108
本文介绍了如何在没有背景的情况下将UIView转换为UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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更改为UIImage

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;
}

嗯..像这样的例子,我有一个矩形作为UIView和一个三角形作为UIView中的一个形状。然后我想让这个UIView成为一个UIImage,但我只想要没有背景的三角形,所以图像只是一个三角形..我怎么能这样做?

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?

推荐答案

UIImage总是矩形的。您可以使用的是视图中的不透明度。如果您将除pin图像之外的所有内容都设为透明,则看起来好像只有一个图像。

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.

使用:

view.backgroundColor = [UIColor clearColor];

使您的视图背景透明。

这篇关于如何在没有背景的情况下将UIView转换为UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆