0

I am using the below code to take a screenshot of the screen, but it replaces the camera view with all black (though all other UI elements are fine-- I need the Camera View as well as UI elements).

All answers to similar questions that I've found are either extremely old/deprecated, or in Swift. If anyone has a simple, Obj-C solution to this problem, it would be much appreciated.

Thanks!

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

[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image);
if (imageData) {
    [imageData writeToFile:@"Screenshot.png" atomically:YES];
    NSLog(@"Screenshot write successful");
} else {
    NSLog(@"Error while taking screenshot");
}
Branch
  • 387
  • 5
  • 16
  • What do you mean Camera view turns black? Are you trying to take a screenshot of the camera? a bit confusing the question – Anjula Serasinghe Jan 07 '20 at 07:53
  • @AnjulaS. Yes, I am trying to programatically take a screenshot of the screen, but the camera view on the screen shows black instead of what the camera is seeing. – Branch Jan 07 '20 at 14:16

1 Answers1

1

If i understand correct, it can help you get video screen - https://stackoverflow.com/a/37789235/1678018
And you can add this image in screen image (which you have with UIGraphicsGetImageFromCurrentImageContext).

Example way for add UIImage on top of another UIImage: Add UIImage on top of another UIImage

Eysner
  • 584
  • 4
  • 15
  • Ok— so let’s say I have the two (an image with the camera, and one with the UI elements of the app), how do I “add this image” and combine them? Is there a simpler way that just gets everything? – Branch Jan 09 '20 at 12:11
  • I think we have not simpler way. You have two image and you can draw video screen into screenshot. For example, like this - https://stackoverflow.com/a/10523275/1678018 – Eysner Jan 10 '20 at 04:30
  • that code about combining is confusing... I don't know what to replace profile and badge with. plus commenters mention it cuts stuff off. Are you able to update your answer to have functioning code that combines the camera with the UI view (UI view taken from code in the original question)? That would help a lot. – Branch Jan 10 '20 at 22:58
  • I figured it out. Awarding bounty for the help. Thanks :) – Branch Jan 12 '20 at 22:18
  • Sorry for long answer, anyway you figured out yet. You're welcome) – Eysner Jan 13 '20 at 04:44