Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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?

like image 255
user4951 Avatar asked Aug 01 '11 06:08

user4951


1 Answers

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.

like image 120
adriaan Avatar answered Sep 28 '22 04:09

adriaan