First, the common progressive loading picture mode
Currently we see the following three implementations of progressive loading:
1) load different sizes of pictures from the web, from small to large. First pull a small thumbnail to do the stretching display, and then pull the medium-sized diagram, pull over the direct overlay display, and finally pull the original image, pull completed after the display of the original.
2) pull the largest picture directly from the Web, and display a little bit of image every bit of data, so it will be refreshed from top to bottom.
3) combined with the 1th and 2nd, pull a thumbnail to do the stretching display, and then use the second method to pull the original image directly, so that the progressive loading can be achieved, but also save a few intermediate network requests.
Today we are going to discuss the Cgimagesource implementation from the Web side of the gradual loading of images, to achieve this goal we need to create a urlconnnection, and then implement the proxy, each time the data received when the image is updated. The following main implementation of the source code:
?
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 8 5 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;
@end
@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); Get the length of a large file NSString *mimeType = response.MIMEType;
NSLog(@ "MIME TYPE %@" , mimeType); //获得文件的类型(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 ; } The application of the method in ImageIO frame CGImageSourceUpdateData(_incrementallyImgSource, (CFDataRef)_recieveData, _isLoadFinished); CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_incrementallyImgSource, 0 , NULL); self.image = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); }
@end </corefoundation></imageio> |
iOS Crazy detailed ImageIO complete progressive loading picture