imageIO 实现渐进加载图片的效果
2014-12-12 14:42
357 查看
话不多说,直接上代码
- (void)viewDidLoad {
[super
viewDidLoad];
// _imageURL = [imageURLS retain];
_request = [[NSURLRequest
alloc] initWithURL:[NSURL
URLWithString:@"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"]];
_conn = [[NSURLConnection
alloc] initWithRequest:_request
delegate:self];
_incrementallyimgSource =
CGImageSourceCreateIncremental(NULL);
_recieveData = [[NSMutableData
alloc] init];
_isLoadFinished =
false;
view = [[UIImageView
alloc] initWithFrame:CGRectMake(10,10,
300, 300)];
view.image =
self.image;
[self.view
addSubview:view];
}
- (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 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);
view.image = [UIImage
imageWithCGImage:imageRef];
// [self.view addSubview:view];
CGImageRelease(imageRef);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (!_isLoadFinished) {
CGImageSourceUpdateData(_incrementallyimgSource, (CFDataRef)_recieveData,
_isLoadFinished);
CGImageRef imageRef =
CGImageSourceCreateImageAtIndex(_incrementallyimgSource,
0, NULL);
view.image = [UIImage
imageWithCGImage:imageRef];
CGImageRelease(imageRef);
[view
setNeedsLayout];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
}
- (void)viewDidLoad {
[super
viewDidLoad];
// _imageURL = [imageURLS retain];
_request = [[NSURLRequest
alloc] initWithURL:[NSURL
URLWithString:@"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"]];
_conn = [[NSURLConnection
alloc] initWithRequest:_request
delegate:self];
_incrementallyimgSource =
CGImageSourceCreateIncremental(NULL);
_recieveData = [[NSMutableData
alloc] init];
_isLoadFinished =
false;
view = [[UIImageView
alloc] initWithFrame:CGRectMake(10,10,
300, 300)];
view.image =
self.image;
[self.view
addSubview:view];
}
- (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 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);
view.image = [UIImage
imageWithCGImage:imageRef];
// [self.view addSubview:view];
CGImageRelease(imageRef);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (!_isLoadFinished) {
CGImageSourceUpdateData(_incrementallyimgSource, (CFDataRef)_recieveData,
_isLoadFinished);
CGImageRef imageRef =
CGImageSourceCreateImageAtIndex(_incrementallyimgSource,
0, NULL);
view.image = [UIImage
imageWithCGImage:imageRef];
CGImageRelease(imageRef);
[view
setNeedsLayout];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
}
相关文章推荐
- jquery.lazyload.js实现图片延迟加载——wordpress图片随滚动条渐显效果
- JavaScript实现页面滚动图片加载(仿lazyload效果)
- CSS3实现的图片加载动画效果
- HTML5实现的图片无限加载的瀑布流效果另带边框圆角阴影
- iOS: imageIO完成渐进加载图片
- jquery.lazyload.js实现图片延迟加载——wordpress图片随滚动条渐显效果
- android 实现图片加载效果
- CSS3实现的图片加载动画效果
- jQuery实现滚动加载图片效果
- imageIO完成渐进加载图片
- jquery.lazyload.js实现图片延迟加载——wordpress图片随滚动条渐显效果
- 纯CSS实现图片预加载效果
- ListView,实现滚动分页效果。实现异步加载URL生成的图片。使得屏幕不卡
- CSS3实现的图片加载动画效果
- js实现图片加载时候逐渐出现的杂色效果
- ListView,实现滚动分页效果。实现异步加载URL生成的图片。使得屏幕不卡
- Android提高篇之自定义dialog实现processDialog“正在加载”效果、使用Animation实现图片旋转
- 实现图片延迟加载——图片随滚动条渐显效果
- iOS: imageIO完成渐进加载图片
- iOS: imageIO完成渐进加载图片