cell中嵌套UIWebView和WKWebView

最开始是Cell中嵌套UIWebView,但是当打开关闭不同网页重复很多次时,内存会不断增加,存在内存泄漏,后来改成cell中嵌套UIWebView和WKWebView

cell中嵌套UIWebView和WKWebView_第1张图片

QYAwardInfoDescribeCell.h的代码:

#import 

@class QYAwardInfoModel;
@interface QYAwardInfoDescribeCell : UITableViewCell
//
@property (nonatomic,strong) QYAwardInfoModel *model;
//
//
@property (nonatomic,strong) void (^webViewFinishLoadBlock)(CGFloat webViewHeight);

@end

QYAwardInfoDescribeCell.m


#import "QYAwardInfoDescribeCell.h"
#import "QYAwardInfoModel.h"



@interface QYAwardInfoDescribeCell () 
//
//@property (weak, nonatomic) IBOutlet UILabel *awardDescribeL;


@property (weak, nonatomic) UIWebView *webView;


@end

@implementation QYAwardInfoDescribeCell


#pragma mark - lifecycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //
        self.webView.delegate = self;
        [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self.contentView);
        }];
    }
    return self;
}

#pragma mark - setModel
- (void)setModel:(QYAwardInfoModel *)model {
    _model = model;
    
    NSString *str = model.descriptionField;
    if (str.length > 0) {
        [self.webView loadHTMLString:str baseURL:nil];
    }
    
}

#pragma mark webView代理
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // 获取内容高度
    NSString *heightStr =  [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"];
    CGFloat height = [heightStr floatValue];
    //
    if (height != self.model.particularsWebViewHeight) {
        self.webViewFinishLoadBlock ? self.webViewFinishLoadBlock(height) : nil;
    }
    
}
   
#pragma mark - Properties' Getter & Setter
- (UIWebView *)webView {
    if (!_webView) {
        UIWebView *webView = [[UIWebView alloc] init];
        webView.scrollView.showsHorizontalScrollIndicator = NO;
        webView.scrollView.showsVerticalScrollIndicator = NO;
        webView.scrollView.scrollEnabled = NO;
        //
        webView.backgroundColor = [UIColor whiteColor];
        webView.opaque = NO;
        //
        _webView = webView;
         [self.contentView addSubview:_webView];
    }
    return _webView;
}
@end

替换为WKWebView后,经过测试发现,同样打开关闭网页界面重复十几次后,使用后者内存增长更小,内存减少20M以上

#import "QYAwardInfoDescribeCell.h"
#import "QYAwardInfoModel.h"

#import 
#import 

@interface QYAwardInfoDescribeCell () 

@property (weak, nonatomic) WKWebView *webView;
@end

@implementation QYAwardInfoDescribeCell


#pragma mark - lifecycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //
        self.webView.navigationDelegate = self;
        [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self.contentView);
        }];
    }
    return self;
}

#pragma mark - setModel
- (void)setModel:(QYAwardInfoModel *)model {
    _model = model;

    NSString *str = model.descriptionField;
    if (str.length > 0) {
        [self.webView loadHTMLString:str baseURL:nil];
    }
    
}

#pragma mark webView代理
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    // 获取内容高度
    [webView evaluateJavaScript:@"document.documentElement.scrollHeight" completionHandler:^(id _Nullable heightStr, NSError * _Nullable error) {
        CGFloat height = [heightStr floatValue];
        //
        if (height != self.model.particularsWebViewHeight) {
            self.webViewFinishLoadBlock ? self.webViewFinishLoadBlock(height) : nil;
        }
    }];
}

- (WKWebView *)webView {
    if (!_webView) {
        WKWebView *webView = [[WKWebView alloc] init];
        webView.scrollView.scrollEnabled = NO;
        //
        webView.backgroundColor = [UIColor whiteColor];
        webView.opaque = NO;
        //
        _webView = webView;
         [self.contentView addSubview:_webView];
    }
    return _webView;
}

@end

你可能感兴趣的:(cell中嵌套UIWebView和WKWebView)