【问题标题】:How to optimize memory usage in UIImage如何优化 UIImage 中的内存使用
【发布时间】:2014-02-24 18:42:32
【问题描述】:

我尝试截取uiwebview 的屏幕截图并将其与观察者一起发送到另一个类中的另一个 UIImageView。 我用这个方法截图:

    -(UIImage*)takeScreenshoot{
    @autoreleasepool {
        UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width,self.view.frame.size.height));
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.webPage.layer renderInContext:context];
        UIImage *__weak screenShot = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return screenShot;
    }

}

但是我有问题。每次我用这种方法截屏时,内存速率都会增长大约 10-15mb 并且它没有意识到这一点。如果我在每个 webviewdidfinishload 中截屏,你可以想象它会占用多少内存!

我该如何解决这个问题?

【问题讨论】:

  • 你不需要这一行:CGContextRef context = UIGraphicsGetCurrentContext();
  • @Greg 这不会让记忆更轻!

标签: ios objective-c uiwebview uiimageview uiimage


【解决方案1】:

如果可能,请尝试使用 [UIScreen snapshotViewAfterScreenUpdates] which returns UIView 。 这是当前显示内容的快照(应用快照)。

即使苹果也说“this method is faster than trying to render the contents of the screen into a bitmap image yourself”。

根据您的代码,您将传递此位图图像以仅显示在其他一些 UIImageView 中。所以我认为在这里使用 UIScreen 方法是合适的。

仅显示 UIWebView 部分->

创建另一个UIView 实例。
将其框架设置为您的 webView 的框架。
现在将此 screenShot 视图作为子视图添加到 createdView 并设置其框架以便显示 webView 部分。

【讨论】:

  • 你不明白。我只需要这个 UiWebView 的截图。
  • 嗯,那更好,但我有标签浏览器,如果 uiwebview 不可见,它就不起作用。 :(
【解决方案2】:

在获得屏幕截图后尝试致电CGContextRelease(context);

或者如@Greg所说,去掉那行直接使用UIGraphicsGetCurrentContext()

【讨论】:

    猜你喜欢
    • 2014-05-05
    • 2011-09-06
    • 2014-04-25
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 2015-12-09
    • 2014-10-02
    • 2016-09-01
    相关资源
    最近更新 更多