0

I know that this code renders a UIView into a UIImage:

- (UIImage *)captureView {
    //hide controls if needed
    CGRect rect = [self.view bounds];

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

However, how to take a snapshot of a view that has multiple UIViews on top of it but only some of these UIViews? For instance, imagine Snapchat. You take a picture, and then you can add emojis, text, and paint as much as you want. Each one of these are separate UIViews. However, when we click on the button to post a snap, Snapchat include everything on the final image except the UI buttons ( like open-emoji-menu button, post button, save to gallery button, write-text button, etc..). It's like as if these UIButtons were hidden on the time the snapshot was taken, but they are not hidden really. So how to achieve this? I believe they don't generate multiple images from every uiview, but rather take a single snapshot (screenshot) but exclude the UIButton from going into the final image. Any ideas anyone, please?

dan
  • 9,695
  • 1
  • 42
  • 40
rgoncalv
  • 5,825
  • 6
  • 34
  • 61
  • instead of self.view, pass in the view param u want to snapshot. – Teja Nandamuri Apr 08 '16 at 18:04
  • @originaluser2, your answer seems right but I don't fully understand it. If I have like 30 UIViews as subviews of my self.view (main view of view controller, which I will call Group A) and I want to only have 20 of these UIViews on group B,that is going to appear on the final image, and hide 10 of these UIViews on group C, that is not going to appear on the final image, which of these groups is your contentView, and viewToHide? – rgoncalv Apr 08 '16 at 18:31
  • @rgoncalv In your `self.view`, you'll have a `contentView`, and at the time of a screenshot, also an `overlayView`. Your views to hide should be a subview(s) of your `contentView` (only so you don't have to mess any further with the actual hierarchy - their actual superview is unimportant). I have updated my answer showing the view hierarchy. – Hamish Apr 08 '16 at 18:37
  • Thanks mate, your new and more elaborated solution cleared my doubts and solved the problem! – rgoncalv Apr 08 '16 at 20:10
  • @originaluser2, just wondering.. Do you have some related code for video as well? In this case the video is being played in an AVPlayerLayer. – rgoncalv Apr 08 '16 at 20:15
  • @rgoncalv I'm afraid I'm not at all experienced with AVFoundation. From what it looks like, you'll have to use `AVAssetImageGenerator` to get out a still image from a given time in the video. Have a look [here](http://stackoverflow.com/questions/26937656/ios-taking-screenshot-presenting-video-ipad) and [here](http://stackoverflow.com/questions/5388226/get-the-screenshot-of-a-streaming-video-on-iphone) – although unfortunately neither answers are particulary well explained. Glad my solution worked for you :) – Hamish Apr 08 '16 at 20:53
  • I think you misunderstood it haha. I am not looking for an image from a video, but to export a video with my UIViews on top of it, similarly with what you answered for pictures. So, If I take a picture, thanks to you, I can now export to the device's gallery my picture with the editings I put on it (like the picture with text). If I record a video instead, I want to save in the gallery the video with whatever I put on top of it (almost like a watermark). It seems I have found a solution. I will link it here if it works. But thank you for all your time and consideration. – rgoncalv Apr 08 '16 at 21:40

0 Answers0