ios 性能优化之SDWebimage与YYSDWebimage

SDWebimage 在内存方面存在很大的内存问题,静态的图片也会出现拉伸问题,动态的gif图内存峰值更加的高,内存释放不会及时,

  • SDWebImage 对gif的处理主要是在UIImage+GIF这个文件里面,利用ImageIO函数创建二进制数据,如下:
#import "UIImage+GIF.h"
#import 

@implementation UIImage (GIF)

+ (UIImage *)sd_animatedGIFWithData:(NSData *)data {
    if (!data) {
        return nil;
    }
// 
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);

// 取得gif的帧数
    size_t count = CGImageSourceGetCount(source);

    UIImage *animatedImage;
//  判断图片数量如果是1 返回静态图片
    if (count <= 1) {
        animatedImage = [[UIImage alloc] initWithData:data];
    }
    else {

// 消耗性能的根源  ,在放入数组中播放的时候,核心动画会在两帧之间自动生成股间动画,十分消耗内存,
        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];
//添加数组
            [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
//释放image对象
            CGImageRelease(image);
        }

        if (!duration) {
            duration = (1.0f / 10.0f) * count;
        }

// 设置动图数组 ,生成动图 
        animatedImage = [UIImage animatedImageWithImages:images duration:duration];
    }

    CFRelease(source);

    return animatedImage;
}

由于SDWebImage通过动图数组来实现动态,造成内存不能及时释放

  • 解决方法
    1 开启时钟 ,每次只加载一张图片,之前的立即释放 2 使用YYWebimage代替

  • SDWebimage -静态图片加载 产生拉伸,如下图黄色表示拉伸,

#import "ViewController.h"
#import "YYWebImage.h"
static NSString *KDGroundLxrCellID = @"KDGroundLxrCellID";
@interface ViewController ()
@property(strong, nonatomic) UITableView * mainTable;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreatMainTable];
    
}


/**
 *  创建maintable
 */
#pragma mark-
#pragma mark- 创建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 代理方法实现
 */

-(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.jianshu.com/p/92d949a6d4e7
* 动态gif效果 注意此时占用内存是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对比,内存消耗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 ()
@property(strong, nonatomic) UITableView * mainTable;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreatMainTable];
    
}


/**
 *  创建maintable
 */
#pragma mark-
#pragma mark- 创建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 代理方法实现
 */

-(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对内存方面的处理好好过于SDWebImage是牺牲内存来换取流畅度,而对于流畅度SDWebImage缺要好于YYWebimage,各有所长吧

你可能感兴趣的:(ios 性能优化之SDWebimage与YYSDWebimage)