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

1、常見漸進加載圖片模式html

目前咱們看到的漸進加載主要有如下三種實現方式:web

1) 依次從web上加載不一樣尺寸的圖片,從小到大。最開始先拉取一個小縮略圖作拉伸顯示,而後拉取中等規格的圖,拉取完畢直接覆蓋顯示,最後拉取原圖,拉取完成後顯示原圖。網絡

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

3)結合第1種和第2種,先拉取一個縮略圖作拉伸顯示,而後採用第二種方法直接拉取原圖,這樣便可以實現漸進加載,也能夠節省幾回中間的網絡請求。框架

今天咱們要討論的是CGImageSource實現從web端漸進加載圖片,要達到這個目的咱們須要建立一個URLConnnection,而後實現代理,每次接收到數據時更新圖片便可。下面主要的實現源碼:atom

?url

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= "" >
 
@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;
 
 
@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);
     //得到大文件的長度

    NSString *mimeType = response.MIMEType;spa

 NSLog(@"MIME TYPE %@", mimeType);.net

    //得到文件的類型(mimetype=image/jpeg ); 

    NSArray *arr = [mimeType componentsSeparatedByString:@"/"];代理

//判斷有沒有image  是否是image類型

     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>
相關文章
相關標籤/搜索