一、常见渐进加载图片模式
目前我们看到的渐进加载主要有以下三种实现方式:
1) 依次从web上加载不同尺寸的图片,从小到大。最开始先拉取一个小缩略图做拉伸显示,然后拉取中等规格的图,拉取完毕直接覆盖显示,最后拉取原图,拉取完成后显示原图。
2)直接从web上拉取最大的图片,每接受一点儿数据就显示一点儿图片,这样就会实现从上到下一点点刷新出来的效果。
3)结合第1种和第2种,先拉取一个缩略图做拉伸显示,然后采用第二种方法直接拉取原图,这样即可以实现渐进加载,也可以节省几次中间的网络请求。
今天我们要讨论的是CGImageSource实现从web端渐进加载图片,要达到这个目的我们需要创建一个URLConnnection,然后实现代理,每次接收到数据时更新图片即可。下面主要的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | // // 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= "" > SvIncrementallyImage () { NSURLRequest *_request; NSURLConnection *_conn; CGImageSourceRef _incrementallyImgSource; NSMutableData *_recieveData; long long _expectedLeght; bool _isLoadFinished; } @property (nonatomic, retain) UIImage *image; @property (nonatomic, retain) UIImage *thumbImage; @implementation SvIncrementallyImage @synthesize 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); //获得大文件的长度
//获得文件的类型(mimetype=image/jpeg );
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 ; } //imageIO框架中方法的运用 CGImageSourceUpdateData(_incrementallyImgSource, (CFDataRef)_recieveData, _isLoadFinished); CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_incrementallyImgSource, 0 , NULL); self.image = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); } @end </corefoundation></imageio> |