Skip to content

Instantly share code, notes, and snippets.

@pegurov
Created June 9, 2017 15:04
Show Gist options
  • Save pegurov/9575cee07cad1bbd5d24da92e07aa2ee to your computer and use it in GitHub Desktop.
Save pegurov/9575cee07cad1bbd5d24da92e07aa2ee to your computer and use it in GitHub Desktop.
Rendering layer in background thread
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