SDWebimage 在內(nèi)存方面存在很大的內(nèi)存問題醉鳖,靜態(tài)的圖片也會出現(xiàn)拉伸問題堕花,動態(tài)的gif圖內(nèi)存峰值更加的高,內(nèi)存釋放不會及時圈暗,
- SDWebImage 對gif的處理主要是在UIImage+GIF這個文件里面掂为,利用ImageIO函數(shù)創(chuàng)建二進制數(shù)據(jù),如下:
#import "UIImage+GIF.h"
#import <ImageIO/ImageIO.h>
@implementation UIImage (GIF)
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data {
if (!data) {
return nil;
}
//
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
// 取得gif的幀數(shù)
size_t count = CGImageSourceGetCount(source);
UIImage *animatedImage;
// 判斷圖片數(shù)量如果是1 返回靜態(tài)圖片
if (count <= 1) {
animatedImage = [[UIImage alloc] initWithData:data];
}
else {
// 消耗性能的根源 员串,在放入數(shù)組中播放的時候勇哗,核心動畫會在兩幀之間自動生成股間動畫,十分消耗內(nèi)存寸齐,
NSMutableArray *images = [NSMutableArray array];
NSTimeInterval duration = 0.0f;
for (size_t i = 0; i < count; i++) {
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
// 累加時常
duration += [self sd_frameDurationAtIndex:i source:source];
//添加數(shù)組
[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
//釋放image對象
CGImageRelease(image);
}
if (!duration) {
duration = (1.0f / 10.0f) * count;
}
// 設置動圖數(shù)組 欲诺,生成動圖
animatedImage = [UIImage animatedImageWithImages:images duration:duration];
}
CFRelease(source);
return animatedImage;
}
由于SDWebImage通過動圖數(shù)組來實現(xiàn)動態(tài),造成內(nèi)存不能及時釋放
解決方法
1 開啟時鐘 渺鹦,每次只加載一張圖片瞧栗,之前的立即釋放 2 使用YYWebimage代替
SDWebimage -靜態(tài)圖片加載 產(chǎn)生拉伸,如下圖黃色表示拉伸海铆,
#import "ViewController.h"
#import "YYWebImage.h"
static NSString *KDGroundLxrCellID = @"KDGroundLxrCellID";
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong, nonatomic) UITableView * mainTable;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self CreatMainTable];
}
/**
* 創(chuàng)建maintable
*/
#pragma mark-
#pragma mark- 創(chuàng)建tableview
-(void)CreatMainTable
{
self.mainTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
self.mainTable.delegate = self ;
self.mainTable.dataSource = self ;
self.mainTable.separatorStyle = UITableViewCellSeparatorStyleNone ;
[self.mainTable registerClass:[UITableViewCell class] forCellReuseIdentifier:KDGroundLxrCellID];
//[self.mainTable registerClass:<#(nullable Class)#> forCellReuseIdentifier:<#(nonnull NSString *)#>]
[self.view addSubview:self.mainTable];
}
/**
* mainTable 代理方法實現(xiàn)
*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:KDGroundLxrCellID];
NSString * str =@"https://d13yacurqjgara.cloudfront.net/users/288987/screenshots/1913272/depressed-slurp-cycle.gif";
NSURL *url =[NSURL URLWithString:str];
UIImage *image =[UIImage imageNamed:@"icon-120.png"];
// [cell.imageView yy_setImageWithURL:url placeholder:image];
[cell.imageView sd_setImageWithURL:url placeholderImage:image];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 107.0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 10.0;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end ```
不懂紅綠色代表的意思參考:http://www.reibang.com/p/92d949a6d4e7
* 動態(tài)gif效果 注意此時占用內(nèi)存是110MB 迹恐,cpu占用率是0
![FCDA658B-5F47-4098-9850-F6CDC436C902.png](http://upload-images.jianshu.io/upload_images/1460277-c7b30f595a5b3d5d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 切換YYWebimage對比,內(nèi)存消耗50MB而cpu占用40%
![5DA31356-0B7B-4E06-B0CA-CB25CD58D2C2.png](http://upload-images.jianshu.io/upload_images/1460277-d704003062ca2dbc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![006A8DF6-36D1-4FD4-AA98-0910C0772B1E.png](http://upload-images.jianshu.io/upload_images/1460277-f2e35c9dc8ae68b7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
import "YYWebImageCell.h"
import "YYWebImage.h"
@implementation YYWebImageCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// @property (nonatomic, readonly, strong, nullable) UIImageView *imageView NS_AVAILABLE_IOS(3_0); // default is nil. image view will be created if necessary.
[self setValue:[[YYAnimatedImageView alloc] init] forKey:@"imageView"];
}
return self;
}```
#import "ViewController.h"
#import "YYWebImage.h"
#import "YYWebImageCell.h"
static NSString *KDGroundLxrCellID = @"KDGroundLxrCellID";
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong, nonatomic) UITableView * mainTable;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self CreatMainTable];
}
/**
* 創(chuàng)建maintable
*/
#pragma mark-
#pragma mark- 創(chuàng)建tableview
-(void)CreatMainTable
{
self.mainTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
self.mainTable.delegate = self ;
self.mainTable.dataSource = self ;
self.mainTable.separatorStyle = UITableViewCellSeparatorStyleNone ;
[self.mainTable registerClass:[YYWebImageCell class] forCellReuseIdentifier:KDGroundLxrCellID];
//[self.mainTable registerClass:<#(nullable Class)#> forCellReuseIdentifier:<#(nonnull NSString *)#>]
[self.view addSubview:self.mainTable];
}
/**
* mainTable 代理方法實現(xiàn)
*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
YYWebImageCell *cell =[tableView dequeueReusableCellWithIdentifier:KDGroundLxrCellID];
NSString * str =@"https://d13yacurqjgara.cloudfront.net/users/288987/screenshots/1913272/depressed-slurp-cycle";
NSURL *url =[NSURL URLWithString:str];
UIImage *image =[UIImage imageNamed:@"icon-120.png"];
[cell.imageView yy_setImageWithURL:url placeholder:image];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 107.0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 10.0;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end```
相比較YYWebimage對內(nèi)存方面的處理好好過于SDWebImage是犧牲內(nèi)存來換取流暢度卧斟,而對于流暢度SDWebImage缺要好于YYWebimage殴边,各有所長吧