I try to take screenshot of uiwebview and send it with observer to another UIImageView in another class. I using this method to take screenshot:

    -(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;
    }

}

But then I have problem. Everytime I take screenshot with this method, memory rate grows about 10-15mb and it's not realising it. And if I take screenshot in every webviewdidfinishload, you can imagine how much it can take memory!

How can I fix that issue?

有帮助吗?

解决方案 2

Try calling CGContextRelease(context); after you have got your screen shot.

Or as @Greg said, remove the line and use UIGraphicsGetCurrentContext() directly

其他提示

If possible try to use [UIScreen snapshotViewAfterScreenUpdates] which returns UIView . This is the snapshot of currently displayed content (snapshot of app).

Even apple also says " this method is faster than trying to render the contents of the screen into a bitmap image yourself."

According to your code, you are passing this bitmap image to just display in some other UIImageView. so i think using UIScreen method is appropriate here.

To display UIWebView part only->

Create another UIView instance.
Set its frame to the frame of your webView.
Now add this screenShot view as subView to createdView and set its frame such that webView portion will be displayed.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top