博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS疯狂详解之imageIO完成渐进加载图片
阅读量:7009 次
发布时间:2019-06-28

本文共 3524 字,大约阅读时间需要 11 分钟。

hot3.png

一、常见渐进加载图片模式

目前我们看到的渐进加载主要有以下三种实现方式:

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);
     //获得大文件的长度

    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
;
    
}
     //imageIO框架中方法的运用
    
CGImageSourceUpdateData(_incrementallyImgSource, (CFDataRef)_recieveData, _isLoadFinished);
    
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_incrementallyImgSource,
0
, NULL);
    
self.image = [UIImage imageWithCGImage:imageRef];
    
CGImageRelease(imageRef);
}
 
@end
</corefoundation></imageio>

转载于:https://my.oschina.net/u/2329800/blog/512375

你可能感兴趣的文章
一次利用nginx漏洞的***事件
查看>>
我的友情链接
查看>>
split()方法的用法!
查看>>
配置applicationContext.xml出现的问题
查看>>
无线网络IP地址冲突问题不再棘手
查看>>
我的友情链接
查看>>
域名扫描工具Fierce
查看>>
Zabbix WMI 监控
查看>>
关于for in和for循环的遍历
查看>>
完成端口(CompletionPort)详解 - 手把手教你玩转网络编程系列之三
查看>>
JSP Struts之HTML标签库详解
查看>>
Hp服务器 raid 磁盘故障数据库数据恢复解决方案
查看>>
运维角度浅谈MySQL数据库优化
查看>>
【Spark亚太研究院系-构建Spark集群-配置Hadoop单机模式并运行Wordcount(2)
查看>>
Java通过POI为Excel添加数据验证
查看>>
修改vim的配色方案
查看>>
程矢Axure夜话:程序员眼中的原型设计视频教程之书到用时方恨少
查看>>
网站降权怎么办
查看>>
esxi 4.x升级至5.0
查看>>
Hibernate中save、persist和saveOrUpdate这三个方法的区别
查看>>