Gar*_*abo 2 ios swift wkwebview xcode9-beta
我注意到WKWebView文档现在列出了一个takeSnapshot被iOS 11和macOS 10.13及更高版本(Xcode 9 beta)支持的方法.
还有人玩过还是实现过?我试图让它在操场上工作,但我不知道从哪里开始?这是一种方法WKWebView吗?
我的代码:
import UIKit
import PlaygroundSupport
import WebKit
let frame = CGRect(x: 0, y: 0, width: 800, height:600)
let web = WKWebView(frame: frame)
let rq = URLRequest(url: NSURL(string: "http://apple.com")! as URL)
web.load(rq)
PlaygroundPage.current.liveView = web
PlaygroundPage.current.needsIndefiniteExecution = true
//Take snapshot?
Run Code Online (Sandbox Code Playgroud)
据我测试,该方法takeSnapshot(with:completionHandler:)实际上作为实例方法存在并按预期工作.
只是它有点难以使用它.
该方法声明其第一个参数为WKSnapshotConfiguration?,但该类WKSnapshotConfiguration未导入import WebKit.您可以传递nil给参数,但要使用该方法,您需要导入该类型WKSnapshotConfiguration.我无法找到任何依赖的子模块进行导入WKSnapshotConfiguration.
因此,如果您想要使用此新功能,则需要使用bridging-header创建一个App项目.(如果您知道如何在Playground中使用桥接头,您可以在其中测试此功能.但我不知道您是否可以或如何.)
{项目名} -Bridging-Header.h:
@import CoreGraphics;
#import <WebKit/WKSnapshotConfiguration.h>
Run Code Online (Sandbox Code Playgroud)
以及ViewController.swift的一个例子:
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let request = URLRequest(url: URL(string: "http://apple.com")!)
webView.load(request)
}
@IBAction func buttonPressed(_ sender: UIButton) {
webView.takeSnapshot(with: nil) {image, error in
if let image = image {
self.imageView.image = image
print("Got snapshot")
} else {
print("Failed taking snapshot: \(error?.localizedDescription ?? "--")")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
(在视图上放置a WKWebView,a UIImageView和a UIButton.)
还有一个,这似乎是WebKit框架的一个bug,你最好向Apple 发送一个bug报告.
| 归档时间: |
|
| 查看次数: |
1380 次 |
| 最近记录: |