Ios swift-异步使用RenderContext时,UILabel文本不是渲染器
我需要从由Ios swift-异步使用RenderContext时,UILabel文本不是渲染器,ios,image,swift,uiview,rendering,Ios,Image,Swift,Uiview,Rendering,我需要从由UIImageView和UILabel组成的自定义视图生成图像 我无法使用新的iOS 7drawViewHierarchyInRect:AfterScreen更新:,因为我在iPhone 6/6+上遇到了一些棘手的问题() 因此,我使用renderInContext:以旧式方式完成了这项工作,虽然效果很好,但速度相当慢。我正在使用图像生成在GMSMapView中显示标记(上帝,我错过了MapKit…),但是由于这些图像生成的滞后,用户体验非常糟糕 因此,我尝试在背景线程中执行图像创建操
UIImageView
和UILabel
组成的自定义视图生成图像
我无法使用新的iOS 7drawViewHierarchyInRect:AfterScreen更新:
,因为我在iPhone 6/6+上遇到了一些棘手的问题()
因此,我使用renderInContext:
以旧式方式完成了这项工作,虽然效果很好,但速度相当慢。我正在使用图像生成在GMSMapView
中显示标记(上帝,我错过了MapKit
…),但是由于这些图像生成的滞后,用户体验非常糟糕
因此,我尝试在背景线程中执行图像创建操作,以获得平滑效果,但问题是:我的大多数标签都没有渲染
有人已经面临这个问题了吗
以下是我使用的代码:
func CGContextCreate(size: CGSize) -> CGContext {
let scale = UIScreen.mainScreen().scale
let space: CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()
let bitmapInfo: CGBitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedFirst.rawValue)
let context: CGContext = CGBitmapContextCreate(nil, Int(size.width * scale), Int(size.height * scale), 8, Int(size.width * scale * 4), space, bitmapInfo)
CGContextScaleCTM(context, scale, scale)
CGContextTranslateCTM(context, 0, size.height)
CGContextScaleCTM(context, 1, -1)
return context
}
func UIGraphicsGetImageFromContext(context: CGContext) -> UIImage? {
let cgImage: CGImage = CGBitmapContextCreateImage(context)
let image = UIImage(CGImage: cgImage, scale: UIScreen.mainScreen().scale, orientation: UIImageOrientation.Up)
return image
}
extension UIView {
func snapshot() -> UIImage {
let context = CGContextCreate(self.frame.size)
self.layer.renderInContext(context)
let image = UIGraphicsGetImageFromContext(context)
return image!
}
func snapshot(#completion: UIImage? -> Void) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
let image = self.snapshot()
completion(image)
}
}
}
我认为问题在于完成是在这个后台队列中执行的。UI更新必须在主线程上,以便可以使用
dispatch_async(dispatch_get_main_queue()) {
completion(image)
}
在主线程之外的其他线程中呈现
UILabel
似乎存在问题。最好的选择是使用
NSString
方法绘制文本