最开始是Cell中嵌套UIWebView,但是当打开关闭不同网页重复很多次时,内存会不断增加,存在内存泄漏,后来改成cell中嵌套UIWebView和WKWebView
QYAwardInfoDescribeCell.h的代码:
#import <UIKit/UIKit.h>
@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 () <UIWebViewDelegate>
//
//@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 <WebKit/WKWebView.h>
#import <WebKit/WKNavigationDelegate.h>
@interface QYAwardInfoDescribeCell () <WKNavigationDelegate>
@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
网友评论