0

I have a UIView which contains another UIView with an UIImage.

    dView=[[drawView alloc]initWithFrame:myUIView.frame];
    [dView newMaskWithColor:[UIColor colorWithPatternImage:chosenImage]];
    [myUIView addSubview:dView];

And by using this code, I erased a part of it, and it looks like this now: enter image description here

and now I added a layer behind this view, and presented another image in that layer.

 [myUIView.layer insertSublayer:_videoPreviewLayer below:dView.layer];

and now it looks like this: (this is the screenshot manually taken on device)

enter image description here

When I try to screenshot the above view programmatically, the result isenter image description here:

I dont why the newly added videopreview layer doesnt appeared in the screenshot.Here is my screenshot method.

-(void)takeSnapShot{

 UIView *topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];  //tried this too

//capture the screenshot of the uiimageview and save it in camera roll
UIGraphicsBeginImageContext(self.myUIView.frame.size);
[self.myUIView.layer renderInContext:UIGraphicsGetCurrentContext()]; // tried this
[self.dView.layer renderInContext:UIGraphicsGetCurrentContext()];   //tried this
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];   //tried this
[topView.layer renderInContext:UIGraphicsGetCurrentContext()];   //tried this
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

}

I am calling this method on a button click in my view controller.

I even tried capturing the window that is suggested from this answer, still I dont get the result I get from screenhotting manually.

Is there any way to capture the top most view that is shown to user ?

Community
  • 1
  • 1
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109

1 Answers1

1

Check out this post: Why don't added sublayers show up in screenshot?

Seems that sublayers aren't handled by the renderInContext method.

When you want to capture a screenshot try storing a frame from the preview layer in a UIImageView then you should be able to manually put that frame underneath your mask and just screenshot the superview.

Community
  • 1
  • 1
john_ryan
  • 1,557
  • 1
  • 16
  • 34