【问题标题】:UIGraphicsBeginImageContextWithOptions very masive memory disasterUIGraphicsBeginImageContextWithOptions 非常巨大的内存灾难
【发布时间】:2014-04-19 19:08:48
【问题描述】:

我有这行代码来截取uiview的截图:

    - (UIImage *)imageWithView:(UIView *)view {
    UIImage *viewImage = nil;
    float height;
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
        height = view.frame.size.height;
    }else{
        height = 730;
    }
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.frame.size.width, height), YES, 0.0);

        [view drawViewHierarchyInRect:CGRectMake(0, -60, view.bounds.size.width, height) afterScreenUpdates:YES];
        viewImage = UIGraphicsGetImageFromCurrentImageContext();


    UIGraphicsEndImageContext();
    return viewImage;
}

...在这个uiview中是UIWebView。

每次 uiwebview 完成加载时我都会调用这个 - (UIImage *)imageWithView:(UIView *)view 方法。还有我的问题:当我打开一些像 google 这样的小网页时,它会占用一些 kb 的内存,但是当我打开像 bbc、cnn、yahoo 或 stackoverflow 这样的大网页时,它可能会占用高达 80mb 的内存。

打开cnn.com时有仪器快照。 几秒钟后它会重新启动,但我不希望使用那么大的内存,因为你可以想象,在这几秒钟内 uiwebview 变得多么无用。

那么,你有什么建议,如何截取uiwebview的屏幕截图,没有那么大的内存使用,我什至不需要这个质量好的uiimage,因为我把它放在120*80 uiimageview中屏幕角。

【问题讨论】:

  • 那是因为contentSize比frame大,尝试用frame size代替

标签: ios objective-c ipad uiview uiwebview


【解决方案1】:

UIGraphicsBeginImageContextWithOptions 中的最后一个参数是比例。如果您知道您只需要 Web 视图的低分辨率渲染,请尝试将比例值设置为小于 1.0。

UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.frame.size.width, height), YES, 0.2);

【讨论】:

    猜你喜欢
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 2013-03-04
    • 2012-03-16
    • 2018-06-02
    • 1970-01-01
    相关资源
    最近更新 更多