iOS imageio nsurlsession 渐进式图片下载
一.圖片常用加載格式分兩種 一般線性式 和交錯/漸進式
自上而下線性式
先模糊再清晰
就概率上而言線性式使用最多,應為他所占空間普片比漸進式小。而這兩種方式對于app端開發人員無需關心,這種圖片存儲格式是由UI來決定的。
舉個? 漸進式圖片保存方式:
1.Photoshop存儲為Progressive JPEG,首先打開一個圖片,選擇“文件 -> 存儲為”,選擇“JPEG”格式,點擊“保存”按鈕。在“JPEG選項”界面的“格式選項”中選擇“連續”,然后在“掃描”選項中設置為“5”。
?
二.iOS代碼實現
在app使用中網卡的情況下如果還是等到圖片下載好了再顯示圖片可能給用戶造成不好的體驗,所以常見的方式就是來了多多給用戶顯示多少。下面通過代碼實現圖片下載一點顯示一點.
?
a.這里首先會用到Imageio相關東西大體介紹下:
1.CGImageSourceRef?
An opaque type that represents an image source.
他的作用就是用來讀取圖片的數據的,image的數據除了圖片數據外還有一些地址位置、設備信息、時間等信息、而這個類就是抽象出來干這個的方便我們獲取到我們關心的數據.
2.CGImagesSourceUpdateData
void CGImageSourceUpdateData(CGImageSourceRef isrc, CFDataRef data, bool final); Description Updates an incremental image source with new data. Parameters isrc An image source. data The data to add to the image source. Each time you call the function CGImageSourceUpdateData, the data parameter must contain all of the image file data accumulated so far. final A value that specifies whether the data is the final set. Pass true if it is, false otherwise.將圖片數據繼續追加到imageSource中去。
isrc imagesource類
data 類型式cfdata可以用nsmutabledata強制轉換,為什么是nsmutabledata可以自己想想。此data必須需從頭到尾一直包含了image信息的data。
final 此次追加的data是不是最后一次,所以告訴我們在追加的過程中要做好判斷,判斷這個數據是不是最后一個.
3.CGImageSourceCreateImageAtIndex
CGImageRef CGImageSourceCreateImageAtIndex(CGImageSourceRef isrc, size_t index, CFDictionaryRef options); Description Creates a CGImage object for the image data associated with the specified index in an image source. Parameters isrc An image source. index The index that specifies the location of the image. The index is zero-based. options A dictionary that specifies additional creation options. See Image Source Option Dictionary Keys for the keys you can supply. Returns Returns a CGImage object. You are responsible for releasing this object using CGImageRelease.通過imagesource轉化出一個cgimage
index表示這個image的位置。默認寫0
options是配置image的特殊信息的,比如kCGImageSourceShouldCache表示可以緩存,默認寫NULL即可。
?
?
b.接下來會用到NSUrlSession的相關功能.這里要拿去到NSURLSessionDataDelegate的兩個回調。
1.請后被相應后的回調
- (void)URLSession:(NSURLSession *)sessiondataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)responsecompletionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler通過他能得到response,通過reponse能夠知道此次請求的數據長度。 內部一定要實現completionHandler(NSURLSessionResponseAllow),否則下面的回調將不會觸發。
2每一次接收到數據的回調
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data通過data來完成一次次給iamgesource的拼裝
?
?下面是代碼實現:
#pragma mark -- 按條漸進式圖片加載 -(void)startLoading:(NSURL *)url {NSURLSessionConfiguration * sessionConfig = [NSURLSessionConfiguration ephemeralSessionConfiguration];NSURLSession * session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];NSURLRequest * request = [NSURLRequest requestWithURL:url];NSURLSessionDataTask * task = [session dataTaskWithRequest:request];[task resume];_recieveData = [[NSMutableData alloc]init]; //用于接受data_imageSourceRef = CGImageSourceCreateIncremental(NULL); //創建可變的sourceref_isLoadingFinished = NO; //用于判斷是否加載完畢 }?
?//接受到響應獲取data總長度
- (void)URLSession:(NSURLSession *)sessiondataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)responsecompletionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {_expectedLength = response.expectedContentLength;if (completionHandler) {completionHandler(NSURLSessionResponseAllow);} }//拼接data生成圖片
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTaskdidReceiveData:(NSData *)data {[_recieveData appendData:data];if (_expectedLength <= _recieveData.length) {_isLoadingFinished = YES;}CGImageSourceUpdateData(_imageSourceRef, (__bridge CFDataRef)_recieveData, _isLoadingFinished);CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_imageSourceRef, 0, NULL);NSLog(@"%@",[NSThread currentThread]);dispatch_sync(dispatch_get_main_queue(), ^{_imageView.image = [UIImage imageWithCGImage:imageRef];});CGImageRelease(imageRef);NSLog(@"loading"); }-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {_isLoadingFinished = YES;if (!_isLoadingFinished) {CGImageSourceUpdateData(_imageSourceRef, (__bridge CFDataRef)_recieveData, _isLoadingFinished);CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_imageSourceRef, 0, NULL);dispatch_sync(dispatch_get_main_queue(), ^{_imageView.image = [UIImage imageWithCGImage:imageRef];});CGImageRelease(imageRef);} }
轉載于:https://www.cnblogs.com/xiaolong-/p/7097665.html
總結
以上是生活随笔為你收集整理的iOS imageio nsurlsession 渐进式图片下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow实战-AlexNet
- 下一篇: ajax 请求调用问题