我正在使用下面的代码截取屏幕截图,但它将相机 View 替换为全黑(尽管所有其他 UI 元素都很好——我需要相机 View 和 UI 元素)。
我发现的所有类似问题的答案都非常陈旧/已弃用,或者在 Swift 中。如果有人对此问题有简单的 Obj-C 解决方案,我们将不胜感激。
谢谢!
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");
}
最佳答案
如果我理解正确,它可以帮助您获得视频屏幕 - https://stackoverflow.com/a/37789235/1678018
您可以将此图像添加到屏幕图像中(使用 UIGraphicsGetImageFromCurrentImageContext
)。
在另一个 UIImage 之上添加 UIImage 的示例方法:Add UIImage on top of another UIImage
关于ios - 带有相机 View 的程序化屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59595381/