Swift代码
当我们拿到一个UIView的截图中,我们使用此代码通常是:迅速UIGraphicsGetImageFromCurrentImageContext不能释放内存
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个月后没有答案,我想我得到了同样的问题。我不认为你找到了解决方法或解决方法? – Martin
我遇到了同样的问题并寻找解决方案。 – Hope