GSL*_*iPt 19 uiimage ios uigraphicscontext swift
当我们获得UIView的屏幕截图时,我们通常使用以下代码:
UIGraphicsBeginImageContextWithOptions(frame.size, false, scale)
drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)
drawViewHierarchyInRect && UIGraphicsGetImageFromCurrentImageContext将在当前上下文中生成图像,但在调用时不会释放 内存UIGraphicsEndImageContext.
内存使用继续增加,直到应用程序崩溃.
虽然有一个词UIGraphicsEndImageContext会CGContextRelease自动调用",但它不起作用.
我该如何释放内存drawViewHierarchyInRect或UIGraphicsGetImageFromCurrentImageContext使用
有没有生成截图没有drawViewHierarchyInRect?
1自动释放:不起作用
var image:UIImage?
autoreleasepool{
UIGraphicsBeginImageContextWithOptions(frame.size, false, scale)
drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
image = nil
Run Code Online (Sandbox Code Playgroud)
2 UnsafeMutablePointer:不起作用
var image:UnsafeMutablePointer<UIImage> = UnsafeMutablePointer.alloc(1)
autoreleasepool{
UIGraphicsBeginImageContextWithOptions(frame.size, false, scale)
drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
image.initialize(UIGraphicsGetImageFromCurrentImageContext())
UIGraphicsEndImageContext()
}
image.destroy()
image.delloc(1)
Run Code Online (Sandbox Code Playgroud)
小智 5
我通过将图像操作放在另一个队列中解决了这个问题!
private func processImage(image: UIImage, size: CGSize, completion: (image: UIImage) -> Void) {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue), 0)) {
UIGraphicsBeginImageContextWithOptions(size, true, 0)
image.drawInRect(CGRect(origin: CGPoint.zero, size: size))
let tempImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
completion(image: tempImage)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6559 次 |
| 最近记录: |