2017-09-04 4 views
2

MacOSでオフスクリーンのCALayerのコンテンツ(アルファが不要)を描画するための最速の方法を探しています。これらの例はスレッド化されていませんが、バックグラウンドスレッドでこの描画を行っているため、(なぜCALayer.setNeedsDisplayを使用しているのかはわかりません)。オフスクリーンのCALayerのコンテンツを表示する最速の方法

私の元のコードは、これをしなかった:

let bounds = layer.bounds.size 
let contents = NSImage(size: size) 
contents.lockFocusFlipped(true) 
let context = NSGraphicsContext.current()!.cgContext 
layer.draw(in: context) 
contents.unlockFocus() 
layer.contents = contents 

答えて

0

私の現在の最高かなり速いです:より良いそれを作るための

let contentsScale = layer.contentsScale 
let width = Int(bounds.width * contentsScale) 
let height = Int(bounds.height * contentsScale) 
let bytesPerRow = width * 4 
let alignedBytesPerRow = ((bytesPerRow + (64 - 1))/64) * 64 

let context = CGContext(
    data: nil, 
    width: width, 
    height: height, 
    bitsPerComponent: 8, 
    bytesPerRow: alignedBytesPerRow, 
    space: NSScreen.main()?.colorSpace?.cgColorSpace ?? CGColorSpaceCreateDeviceRGB(), 
    bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue 
)! 

context.scaleBy(x: contentsScale, y: contentsScale) 
layer.draw(in: context) 
layer.contents = context.makeImage() 

ヒントと推奨事項は/速く歓迎されています。

関連する問題