iOS瘋狂詳解之imageIO完成漸進載入圖片

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   使用   sp   strong   on   

一、常見漸進載入圖片模式

目前我們看到的漸進載入主要有以下三種實現方式:

1)  依次從web上載入不同尺寸的圖片,從小到大。最開始先拉取一個小縮圖做展開顯示,然後拉取中等規格的圖,拉取完畢直接覆蓋顯示,最後拉取原圖,拉取完成後顯示原圖。

2)直接從web上拉取最大的圖片,每接受一點兒資料就顯示一點兒圖片,這樣就會實現從上到下一點點重新整理出來的效果。

3)結合第1種和第2種,先拉取一個縮圖做展開顯示,然後採用第二種方法直接拉取原圖,這樣即可以實現漸進載入,也可以節省幾次中間的網路請求。

二、通過imageIO實現圖片的漸進載入

imageIO的Guide中原話是這麼說的: 

"If you have a very large image, or are loading image data over the web, you may want to create an incremental image source so that you can draw the image data as you accumulate it. " 

翻譯過來就是:

 "如果你想載入一副特別大的圖片,或者從網路上載入一副圖片,你可以通過建立一個imageSource實現漸進載入的效果。"

翻譯的不是很地道,大概就是這麼個意思,以前在做PowerCam的時候,當時為了在iOS上處理超大圖的時候也試過這種方法,當時測試使用的是一副中國地圖,解析度為10000*8000的,結果是當整幅圖片載入到記憶體時,記憶體吃不消,於是就放棄了。現在想想對於這種超大圖片的處理,我們可以採用分區的方式進行,每次只需要處理一小塊圖片即可,這個問題就留給大家思考吧。

今天我們要討論的是CGImageSource實現從web端漸進載入圖片,要達到這個目的我們需要建立一個URLConnnection,然後實現代理,每次接收到資料時更新圖片即可。下面主要的實現源碼:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 ////  SvIncrementallyImage.m//  SvIncrementallyImage////  Created by  maple on 6/27/13.//  Copyright (c) 2013 maple. All rights reserved.//#import "SvIncrementallyImage.h"#import <imageio imageio.h="">#import <corefoundation corefoundation.h="">@interface SvIncrementallyImage () {    NSURLRequest    *_request;    NSURLConnection *_conn;         CGImageSourceRef _incrementallyImgSource;         NSMutableData   *_recieveData;    long long       _expectedLeght;    bool            _isLoadFinished;} @property (nonatomic, retain) UIImage *image;@property (nonatomic, retain) UIImage *thumbImage;@[email protected] [email protected] imageURL = _imageURL;@synthesize image    = _image;@synthesize thumbImage = _thumbImage;- (id)initWithURL:(NSURL *)imageURL{    self = [super init];    if (self) {        _imageURL = [imageURL retain];                 _request = [[NSURLRequest alloc] initWithURL:_imageURL];        _conn    = [[NSURLConnection alloc] initWithRequest:_request delegate:self];                 _incrementallyImgSource = CGImageSourceCreateIncremental(NULL);                 _recieveData = [[NSMutableData alloc] init];        _isLoadFinished = false;    }        return self;}#pragma mark -#pragma mark NSURLConnectionDataDelegate - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    _expectedLeght = response.expectedContentLength;    NSLog(@"expected Length: %lld", _expectedLeght);         NSString *mimeType = response.MIMEType;    NSLog(@"MIME TYPE %@", mimeType);         NSArray *arr = [mimeType componentsSeparatedByString:@"/"];    if (arr.count < 1 || ![[arr objectAtIndex:0] isEqual:@"image"]) {        NSLog(@"not a image url");        [connection cancel];        [_conn release]; _conn = nil;    }}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    NSLog(@"Connection %@ error, error info: %@", connection, error);}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{    NSLog(@"Connection Loading Finished!!!");        // if download image data not complete, create final image    if (!_isLoadFinished) {        CGImageSourceUpdateData(_incrementallyImgSource, (CFDataRef)_recieveData, _isLoadFinished);        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_incrementallyImgSource, 0, NULL);        self.image = [UIImage imageWithCGImage:imageRef];        CGImageRelease(imageRef);    }}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [_recieveData appendData:data];         _isLoadFinished = false;    if (_expectedLeght == _recieveData.length) {        _isLoadFinished = true;    }         CGImageSourceUpdateData(_incrementallyImgSource, (CFDataRef)_recieveData, _isLoadFinished);    CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_incrementallyImgSource, 0, NULL);    self.image = [UIImage imageWithCGImage:imageRef];    CGImageRelease(imageRef);}@end</corefoundation></imageio>

從上面代碼中我們可以看到,一開始我們根據傳入的URL建立一個URLConnection,同時建立一個空的CGImageSource,然後在每次收到資料的時候調用CGImageSourceUpdateData更新imageSource的資料,接著調用CGImageSourceCreateImageAtIndex擷取最新的圖片即可。

怎麼樣,看到上面的實現是不是感覺實現從web上漸進載入圖片很簡單,雖然imageIO幫我們做了很多事情,但是我們也應該瞭解它的原理。我們知道檔案都是有格式的,一般檔案的頭部會記錄一些關於檔案格式的資料,後面就是實際的檔案資料。

拿最簡單的BMP圖片檔案舉例:

1)  最開始的BITMAPFILEHEADER,這部分主要記錄檔案的大小,以及實際的映像資料離檔案頭的距離。

2)  接著是BITMAPINFOHEADER,這部分主要記錄圖片的寬,高,位深等資訊

3)可選的調色盤資訊

4)最後一部分就是實際的圖片資料。

前三部分的資訊很小,一般加起來不會超過100個位元組,擷取到這寫資訊以後,我們就可以很輕鬆的根據後面的資料構建出圖片,當資料擷取的越來越完整的時候,我們構造出的圖片就會越完整,直至全部載入完成。

BMP格式是簡單的圖片格式,其他的JPG,PNG雖然結果更加複雜,但是總體構成都差不多。imageIO正是協助我們完成了眾多圖片格式的編解碼,然後一步步構造出最終的圖片。

 

轉寄自: http://blog.csdn.net/wanglongblog/article/details/41868499

iOS瘋狂詳解之imageIO完成漸進載入圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.