小编GSL*_*iPt的帖子

swift UIGraphicsGetImageFromCurrentImageContext无法释放内存

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.

内存使用继续增加,直到应用程序崩溃.

虽然有一个词UIGraphicsEndImageContextCGContextRelease自动调用",但它不起作用.

我该如何释放内存drawViewHierarchyInRectUIGraphicsGetImageFromCurrentImageContext使用

要么?

有没有生成截图没有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)

uiimage ios uigraphicscontext swift

19
推荐指数
1
解决办法
6559
查看次数

标签 统计

ios ×1

swift ×1

uigraphicscontext ×1

uiimage ×1