您可以通过设置使用 webview 的应用程序的默认值来启用优化渲染。类似于启用 WebDeveloperExtras,您可以使用它defaults write
来启用加速画布渲染。对我来说,以下标志提高了性能:
defaults write <bundle.id> WebKitAccelerated2dCanvasEnabled -bool true
defaults write <bundle.id> WebKitAcceleratedDrawingEnabled -bool true
defaults write <bundle.id> WebKitCanvasUsesAcceleratedDrawing -bool true
或者,您可以将私有方法添加到WebPreferences
接口:
@interface WebPreferences (WebPrivate)
...
- (BOOL)accelerated2dCanvasEnabled;
- (void)setAccelerated2dCanvasEnabled:(BOOL)enabled;
- (BOOL)acceleratedDrawingEnabled;
- (void)setAcceleratedDrawingEnabled:(BOOL)enabled;
- (BOOL)canvasUsesAcceleratedDrawing;
- (void)setCanvasUsesAcceleratedDrawing:(BOOL)enabled;
- (BOOL)acceleratedCompositingEnabled;
- (void)setAcceleratedCompositingEnabled:(BOOL)enabled;
...
@end
然后以编程方式使用它:
WebPreferences* prefs = [webView preferences];
[prefs setAccelerated2dCanvasEnabled:YES];
[prefs setAcceleratedDrawingEnabled:YES];
[prefs setCanvasUsesAcceleratedDrawing:YES];
[prefs setAcceleratedCompositingEnabled:YES];
[webView setPreferences:prefs];