3

I added a mask to UIView's layer:

CGImageRef maskImageRef = [UIImage imageNamed:"Icon.png"].CGImage; 
CALayer maskLayer = [CALayer layer];
maskLayer.contents = (__bridge id)maskImageRef;
self.layer.mask = maskLayer;

Then I use this code to get snapshot from a UIView:

[self.layer renderInContext:mainViewContentContext];

But the mask wasn't drawn.

How to draw self.layer with mask?

Special thx!

OpenThread
  • 2,096
  • 3
  • 28
  • 49
  • possible duplicate of [layer.renderInContext doesn't take layer.mask into account?](http://stackoverflow.com/questions/4896296/layer-renderincontext-doesnt-take-layer-mask-into-account) – rob mayoff Jun 18 '12 at 06:26
  • layerA.renderInContext will not render layerA's mask layer in context – OpenThread Jun 27 '12 at 02:04

2 Answers2

2

One way to fix this can be to use Quartz functions

Use CGContextClipToMask(context, rect, maskImage) inside drawRect method

Refer CGContextClipToMask example link for help

Community
  • 1
  • 1
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • http://stackoverflow.com/questions/1344767/how-to-draw-a-shape-on-top-of-a-uiimage-while-respecting-the-images-alpha-mask – Paresh Navadiya Jun 18 '12 at 07:15
  • It's a lot of work doing this. i must handle transforms on mask layer, main layer and view by my self.i really used four days draw them with quartzcore. – OpenThread Jun 27 '12 at 02:07
2

You have to screenshot of the screen try this code:

CGRect screenRect =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44); 
UIGraphicsBeginImageContext(screenRect.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:ctx];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);

i hope help you.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
Dhaval Bhadania
  • 3,090
  • 1
  • 20
  • 35