【发布时间】:2015-09-08 16:53:01
【问题描述】:
Swift 代码
当我们得到一个 UIView 的截图时,我们通常使用这个代码:
UIGraphicsBeginImageContextWithOptions(frame.size, false, scale)
drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
问题
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
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)
【问题讨论】:
-
我看到你在 2 个月后没有答案,我想我遇到了同样的问题。我想您没有找到解决方法或解决方法?
-
我也遇到了同样的问题,正在寻找解决方案。
-
过去几周我遇到了同样的问题。有人修了吗?
标签: ios swift uiimage uigraphicscontext