uoz 作業日記

様々な作業の記録を共有するブログです。

How to take sreencapture (screenshot) of WebView for OSX app (swift) | OSXのSwiftアプリでスクリーンショットを取る方法(webViewのiframe内)

I found two ways.
ググりまくって、2つの方法を見つけた。iOSの場合は多いが、OSXアプリの情報は少ない。

Using WKWebView | WKWebViewを使う方法

References
https://github.com/lemonmojo/WKWebView-Screenshot
http://qiita.com/edo_m18/items/3df1861dfb837838d33b(timer)

let delay = 3.0 * Double(NSEC_PER_SEC)
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

let image = self.webView!.captureScreenshotWithTimeout(timeout)

"self.webView" is a instance of WKWebView.

This method works, but can not take contents inside iframe.

Another problem is that this code use private API, so you cannot publish apps through the App Store using this codde.

ただし、この方法だとiFrame内が真っ黒になってとれない。

Using screencapture command | OSXのscreencaptureコマンドを使う方法

References
https://github.com/leemachin/say-cheese

 system("screencapture -c -x");
 let imageFromClipboard = NSImage(pasteboard: NSPasteboard.generalPasteboard())

This code can get iframe content.
However, you have to crop image if you need only a part of the whole screenshot.

  • R option allows you to take a part of the whole screen.

http://qiita.com/South_STR/items/f4624134726eca839647