您的位置:首页 > 职场人生

SDWebImage的底层介绍以及——需要了解和掌握的要点(包括面试题,图片格式,以及GIF的播放)

2015-10-25 11:46 651 查看


具体如下所示:

官方网站
$ git clone --recursive https://github.com/rs/SDWebImage.git
1> 面试题
1]如何防止一个url对应的图片重复下载
* "cell下载图片思路 –有沙盒缓存"

2] SDWebImage的默认缓存时长是多少?
* 1个星期

3] SDWebImage底层是怎么实现的?
* 上课PPT的"cell下载图片思路 –有沙盒缓存"

2>常用方法
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock
completed:(SDWebImageCompletionBlock)completedBlock;

3>内存处理:当app接收到内存警告时,SDWebImage做了什么? 在SDWebImageCache.m中可以找到
- SDWebImage会监听系统的UIApplicationDidReceiveMemoryWarningNotification通知,一旦收到通知,就会清理内存
- 应用程序将要终止的通知,UIApplicationWillTerminateNotification,清理磁盘
- 应用程序进入后台的通知,UIApplicationDidEnterBackgroundNotification,也会清理磁盘

4> SDWebImageOptions
* SDWebImageRetryFailed :下载失败后,会自动重新下载
* SDWebImageLowPriority :当正在进行UI交互时,自动暂停内部的一些下载操作
* SDWebImageRetryFailed | SDWebImageLowPriority :拥有上面2个功能
// 图片的格式
PNG: 无损压缩!压缩比较低。 PNG图片一般会JPG大。
- GPU解压缩的消耗非常小。解压缩的速度比较快,比较清晰,苹果推荐使用
JPG: 有损压缩!压缩比非常高!照相机使用。
- GPU解压缩的消耗比较大
GIF: 可动画的图片
BMP: (位图),没有任何压缩。几乎不用
关于SDwebImage中枚举参数options有以下几种,用到时可以查阅

"留待查阅"
typedefNS_OPTIONS(NSUInteger, SDWebImageOptions) {
/**
* By default, when a URL fail to be downloaded, the URL is blacklisted so the library won't keep trying.
* 默认情况下,当一个 URL下载失败,会将该 URL放在黑名单中,不再尝试下载
*
* This flag disable this blacklisting.
* 此标记取消黑名单
*/
SDWebImageRetryFailed =1 <<0,

/**
* By default, image downloads are started during UI interactions, this flags disable this feature,
* 默认情况下,在 UI交互时也会启动图像下载,此标记取消这一特性
*
* leading to delayed download on UIScrollView deceleration for instance.
* 会推迟到滚动视图停止滚动之后再继续下载(这个注释是假的!)
*/
SDWebImageLowPriority =1 <<1,

/**
* This flag disables on-disk caching
* 此标记取消磁盘缓存,只有内存缓存
*/
SDWebImageCacheMemoryOnly =1 <<2,

/**
* This flag enables progressive download, the image is displayed progressively during download as a browser would do.
* 此标记允许渐进式下载,就像浏览器中那样,下载过程中,图像会逐步显示出来
*
* By default, the image is only displayed once completely downloaded.
* 默认情况下,图像会在下载完成后一次性显示
*/
SDWebImageProgressiveDownload =1 <<3,

/**
* Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
* 即使图像被缓存,遵守 HTPP响应的缓存控制,如果需要,从远程刷新图像
*
* The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
* 磁盘缓存将由 NSURLCache处理,而不是 SDWebImage,这会对性能有轻微的影响
*
* This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
* 此选项有助于处理同一个请求 URL的图像发生变化
*
* If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
* 如果缓存的图像被刷新,会调用一次 completion block,并传递最终的图像
*
* Use this flag only if you can't make your URLs static with embeded cache busting parameter.
* 仅在无法使用嵌入式缓存清理参数确定图像 URL时,使用此标记
*/
SDWebImageRefreshCached =1 <<4,

/**
* In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for
* 在 iOS 4+,当 App进入后台后仍然会继续下载图像。这是向系统请求额外的后台时间以保证下载请求完成的
*
* extra time in background to let the request finish. If the background task expires the operation will be cancelled.
* 如果后台任务过期,请求将会被取消
*/
SDWebImageContinueInBackground =1 <<5,

/**
* Handles cookies stored in NSHTTPCookieStore by setting
* 通过设置
* NSMutableURLRequest.HTTPShouldHandleCookies = YES;
* 处理保存在 NSHTTPCookieStore中的 cookies
*/
SDWebImageHandleCookies =1 <<6,
/**
* Enable to allow untrusted SSL ceriticates.
* 允许不信任的 SSL证书
*
* Useful for testing purposes. Use with caution in production.
* 可以出于测试目的使用,在正式产品中慎用
*/
SDWebImageAllowInvalidSSLCertificates =1 << 7,

/**
* By default, image are loaded in the order they were queued. This flag move them to
* 默认情况下,图像会按照在队列中的顺序被加载,此标记会将它们移动到队列前部立即被加载
*
* the front of the queue and is loaded immediately instead of waiting for the current queue to be loaded (which
* 而不是等待当前队列被加载,等待队列加载会需要一段时间
* could take a while).
*/
SDWebImageHighPriority =1 <<8,

/**
* By default, placeholder images are loaded while the image is loading. This flag will delay the loading
* 默认情况下,在加载图像时,占位图像已经会被加载。而此标记会延迟加载占位图像,直到图像已经完成加载
*
* of the placeholder image until after the image has finished loading.
*/
SDWebImageDelayPlaceholder =1 <<9,

/**
* We usually don't call transformDownloadedImage delegate method on animated images,
* 通常不会在可动画的图像上调用 transformDownloadedImage代理方法,因为大多数转换代码会破坏动画文件
*
* as most transformation code would mangle it.
* Use this flag to transform them anyway.
* 使用此标记尝试转换
*/
SDWebImageTransformAnimatedImage =1 <<10,
};


SDWebImage播放GIF

//
//  ViewController.m
//  SDWebImage播放GIF
//
//  Created by apple on 15/10/25.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"
@interface ViewController ()
@property(nonatomic, strong) UIImageView *imageVIew;
@end

@implementation ViewController

-(void)viewDidLoad
{
self.imageVIew = [[UIImageView alloc] initWithFrame:self.view.bounds ];
[self.view addSubview:self.imageVIew];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.imageVIew sd_setImageWithURL:[[NSBundle mainBundle] URLForResource:@"bd698b0fjw1e0ph8unyg6g.gif" withExtension:nil]];
}

/**
PNG: 无损压缩! 压缩程度没有JPG高。PNG图片一般比JPG大,比较清晰,解压速度比较快, GPU解压消耗比较小,苹果推荐使用。
JPG:有损压缩!压缩比非常高!照相机使用 GPU压缩的消耗比较大
GIF: 可动画的图片
BMP: 位图 没有任何压缩,无论如何放大都不会失真。开发中一般不使用。
*/
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: