Created
June 9, 2017 15:04
-
-
Save pegurov/9575cee07cad1bbd5d24da92e07aa2ee to your computer and use it in GitHub Desktop.
Rendering layer in background thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let selfWidth = Int(width) | |
let selfHeight = Int(height) | |
let selfSize = CGSize(width: selfWidth, height: selfHeight) | |
let title = advertisementTitle | |
SearchPageAdvertisementView.renderQueue.async { | |
let asyncLayer = SearchPageAdvertisementLayer() | |
asyncLayer.title = title | |
asyncLayer.drawsAsynchronously = true | |
asyncLayer.frame = CGRect(origin: .zero, size: CGSize(width: selfWidth, height: selfHeight)) | |
UIGraphicsBeginImageContextWithOptions(selfSize, true, 0) | |
if let context = UIGraphicsGetCurrentContext() { | |
asyncLayer.render(in: context) | |
let layerImage = UIGraphicsGetImageFromCurrentImageContext() | |
if let cgImage = layerImage?.cgImage { | |
DispatchQueue.main.async { [weak self] in | |
self?.layer.contents = cgImage | |
} | |
} | |
} | |
UIGraphicsEndImageContext() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment