1、加载大图,内存崩溃
问题描述:在加载多张高清大图时,会出现崩溃的现象。
解决方案:客户端在显示缩略图时,将宽度大于320的图片,裁剪为宽度为320,高度等比例缩小的图片。
//代码调用方式
[imgView setImageWithURL:[NSURL URLWithString:imageUrl]
placeholder:DefaultIcon
options:kNilOptions
manager:[MGCircleHelper bigImageManager]
progress:nil
transform:nil
completion:nil];
//MGCircleHelper.m
static const CGFloat MGCircleHelperTransformWidthValue = 320;
@implementation MGCircleHelper
+ (YYWebImageManager *)bigImageManager {
static YYWebImageManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *path = [[UIApplication sharedApplication].cachesPath stringByAppendingPathComponent:@"migu.bigImage"];
YYImageCache *cache = [[YYImageCache alloc] initWithPath:path];
manager = [[YYWebImageManager alloc] initWithCache:cache queue:[YYWebImageManager sharedManager].queue];
manager.sharedTransformBlock = ^(UIImage *image, NSURL *url) {
if (!image || image.size.width <= MGCircleHelperTransformWidthValue) {
return image;
}
CGFloat aspectRatio = image.size.width / image.size.height;
CGFloat resultHeight = MGCircleHelperTransformWidthValue / aspectRatio;
return [image imageByResizeToSize:CGSizeMake(MGCircleHelperTransformWidthValue, resultHeight)];
};
});
return manager;
}
@end
2、截图,出现内存泄漏问题
问题描述:排查内存泄漏时,发现网上通用的截屏实现方式,会出现内存泄漏
解决方案:修改截屏的实现方式
- (UIImage *)screenshot {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIGraphicsBeginImageContextWithOptions(window.bounds.size, YES, [UIScreen mainScreen].scale);
[window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}