ios 性能優(yōu)化之SDWebimage與YYSDWebimage

SDWebimage 在內(nèi)存方面存在很大的內(nèi)存問題,靜態(tài)的圖片也會出現(xiàn)拉伸問題,動態(tài)的gif圖內(nèi)存峰值更加的高,內(nèi)存釋放不會及時,

  • SDWebImage 對gif的處理主要是在UIImage+GIF這個文件里面,利用ImageIO函數(shù)創(chuàng)建二進(jìn)制數(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è)置動圖數(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.lxweimin.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,各有所長吧
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容