我发现了这段代码:
func screenShotMethod() {
//Create the UIImage
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
Run Code Online (Sandbox Code Playgroud)
我需要做些什么才能将所有其他元素(如导航栏)添加到屏幕截图中?
如何通过代码(在Swift中)从我的屏幕截取屏幕截图并保存?我可以截取屏幕并将其保存为图像吗?
我看了,我看到了这段代码,但我不能使用它(我认为),因为它没有做任何事情.
var screen = UIScreen.mainScreen()
snapshotVieww = screen.snapshotViewAfterScreenUpdates(false)
UIGraphicsBeginImageContextWithOptions(screen.bounds.size, false, 0)
snapshotVieww.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
provino = UIImageView(image: image)
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Mac 上使用 Xamarin 和 C# 抓取屏幕截图并将其保存在磁盘上。我写了下面的代码:
public static void TakeScreenshotAndSaveToDisk(string path)
{
var fullScreenBounds = NSScreen.MainScreen.Frame;
IntPtr ptr = CGWindowListCreateImage(fullScreenBounds, CGWindowListOption.OnScreenAboveWindow, 0, CGWindowImageOption.Default);
var cgImage = new CGImage(ptr);
var fileURL = new NSUrl(path, false);
var imageDestination = CGImageDestination.Create(new CGDataConsumer(fileURL), UTType.PNG, 1);
imageDestination.AddImage(cgImage);
imageDestination.Close();
imageDestination.Dispose();
fileURL.Dispose();
cgImage.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
该方法执行并且文件出现在正确的位置。如果我尝试打开它,它将显示空白。如果我单击文件上的“获取信息”,它将不会显示预览。关闭应用程序后,可以打开图像并“获取信息”显示预览。
我在这里做错了什么?在我看来,即使我对对象调用 Dispose() ,资源也没有被释放。
谢谢。
我正在尝试从屏幕上获取显示器指定部分的像素颜色。我曾经CGDIsplayCreateImage(_:)这样做过,但由于某种原因,它没有创建当前打开的窗口的屏幕截图,而是只提供了壁纸和任务栏的图像。
为了可视化屏幕截图,我使用了 SwiftUI 视图。在我的应用程序中,我实际上不需要显示屏幕截图。
struct ScreenshotView: View {
let cgImage: CGImage
var body: some View {
Image(decorative: cgImage, scale: 3)
}
}
Run Code Online (Sandbox Code Playgroud)
这些applicationDidFinishLaunching(_:)方法看起来像这样。
func applicationDidFinishLaunching(_ aNotification: Notification) {
let contentRect = NSRect(x: 0, y: 0, width: 240, height: 240)
// Create the window and set the content view.
window = NSWindow(
contentRect: contentRect,
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: ScreenshotView(cgImage: CGDisplayCreateImage(CGMainDisplayID())!))
window.makeKeyAndOrderFront(nil)
}
Run Code Online (Sandbox Code Playgroud)
由于 XCode 和其他一些应用程序已打开,因此它应该为我提供所有打开的 …