Search code examples
iphoneiosobjective-cuiimagecalayer

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?


Solution

  • 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.