JustAnotherCoder
JustAnotherCoder

Reputation: 2575

Save UIView with CALayer.mask as UIImage in iOS 6

I have the following code:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    UIGraphicsBeginImageContextWithOptions(mainView.bounds.size, NO, [UIScreen mainScreen].scale);
}
else {
    UIGraphicsBeginImageContext(mainView.bounds.size);
}

[mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

In mainView, there is a masked subview that does not appear in saveImage when using this method. However, I understand there used to be a UIGetScreenImage method pre iOS 4 that did capture such activity. My question is, what is the best way to capture CALayer activities in iOS 6? Is UIGetScreenImage still private?

Upvotes: 3

Views: 1173

Answers (2)

Andrei Stanescu
Andrei Stanescu

Reputation: 6383

I think there was a similar question about a week ago: Mask does not work when capturing a uiview to a uiimage

On iOS6 there is a problem capturing a UIView with the mask applied (btw, in iOS 7 it has been fixed): you capture the image but the mask is not applied.

I posted a lengthy solution which involved applying the mask manually to the captured image. It's not very difficult and I also made a demo project of this. You can download it here:

https://bitbucket.org/reydan/so_imagemask

If I did not understand your problem correctly, please tell me so I can remove this answer.

Upvotes: 2

vfn
vfn

Reputation: 6066

try getting the presentation layer instead, as it will contain the layer's state.

[mainView.layer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()];

https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instm/CALayer/presentationLayer

Upvotes: 0

Related Questions