ios - 带有相机 View 的程序化屏幕截图

标签 ios objective-c ios13 avcapture

我正在使用下面的代码截取屏幕截图,但它将相机 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/

相关文章:

Iphone QTVR(QuickTime 虚拟现实)支持

ios - CellForRowAtIndexPath 中给出的 UIButton 标记仅给出自定义 UITableViewCell 中的最后一个标记值

ios - MFMailComposeViewController 在 iOS 13 模拟器和设备中的行为不同

使用 UISearchController 时,iOS 13 UIBarButtonItem 不可点击且与 UINavigationBars 重叠

objective-c - "NSSet allObjects"是否随机排序?

ios - 由于 "Library not loaded: @rpath/libswiftCore.dylib",Swift 应用仅在 App Store 构建中崩溃

ios - SwiftUI - 防止用户在文本编辑器中添加新行 (\n)

ios - 创建 UIViewController 时什么时候使用 init()?

objective-c - 在两个 UILabel 之间分割文本字符串以环绕图像

ios - UIPrintInteractionController 不显示多个页面