在iOS开发过程中经常需要通过网络请求加载图片,有时,需要在创建UIImageView或UIButton来显示图片之前需要提前知道图片的尺寸,根据图片尺寸创建对应大小的控件。但是对于网络图片来说,要想通过最优的方法获得尺寸就略微有点困难,大体思路就是下面这种:
如果有使用SDWebImage,则首先检查是否缓存过该图片,如果没有,先通过文件头获取图片大小(针对格式为png、gif、jpg文件获取其尺寸大小),如果获取失败,则下载完整的图片data,然后计算大小,如果有使用SDWebImage,则使用SDWebImage缓存该图片。
实例代码:
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
+(CGSize)downloadImageSizeWithURL:(id)imageURL { NSURL* URL = nil; if ([imageURL isKindOfClass:[NSURL class ]]){ URL = imageURL; } if ([imageURL isKindOfClass:[NSString class ]]){ URL = [NSURL URLWithString:imageURL]; } if (URL == nil) return CGSizeZero; #ifdef dispatch_main_sync_safe if ([[SDImageCache sharedImageCache] diskImageExistsWithKey:absoluteString]) { UIImage* image = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:absoluteString]; if (!image) { NSData* data = [[SDImageCache sharedImageCache] performSelector:@selector(diskImageDataBySearchingAllPathsForKey:) withObject:URL.absoluteString]; image = [UIImage imageWithData:data]; } if (image) { return image.size; } } #endif NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1]; NSString* pathExtendsion = [URL.pathExtension lowercaseString]; CGSize size = CGSizeZero; if ([pathExtendsion rangeOfString:@ "png" ].location != NSNotFound) { size = [self downloadPNGImageSizeWithRequest:request]; } else if ([pathExtendsion rangeOfString:@ "gif" ].location != NSNotFound) { size = [self downloadGIFImageSizeWithRequest:request]; } else { size = [self downloadJPGImageSizeWithRequest:request]; } if (CGSizeEqualToSize(CGSizeZero, size)) { NSData* data = [NSData dataWithContentsOfURL:URL]; UIImage* image = [UIImage imageWithData:data]; if (image) { //如果未使用SDWebImage,则忽略;缓存该图片 #ifdef dispatch_main_sync_safe [[SDImageCache sharedImageCache] storeImage:image recalculateFromImage:YES imageData:data forKey:URL.absoluteString toDisk:YES]; #endif size = image.size; } } //过滤掉不符合大小的图片,大图太大浪费流量,用户体验不好 if (size.height > 2048 || size.height <= 0 || size.width > 2048 || size.width <= 0 ) { return CGSizeZero; } else { return size; } } +(CGSize)downloadPNGImageSizeWithRequest:(NSMutableURLRequest*)request { [request setValue:@ "bytes=16-23" forHTTPHeaderField:@ "Range" ]; NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; if (data.length == 8) { int w1 = 0, w2 = 0, w3 = 0, w4 = 0; [data getBytes:&w1 range:NSMakeRange(0, 1)]; [data getBytes:&w2 range:NSMakeRange(1, 1)]; [data getBytes:&w3 range:NSMakeRange(2, 1)]; [data getBytes:&w4 range:NSMakeRange(3, 1)]; int w = (w1 << 24) + (w2 << 16) + (w3 << 8) + w4; int h1 = 0, h2 = 0, h3 = 0, h4 = 0; [data getBytes:&h1 range:NSMakeRange(4, 1)]; [data getBytes:&h2 range:NSMakeRange(5, 1)]; [data getBytes:&h3 range:NSMakeRange(6, 1)]; [data getBytes:&h4 range:NSMakeRange(7, 1)]; int h = (h1 << 24) + (h2 << 16) + (h3 << 8) + h4; return CGSizeMake(w, h); } return CGSizeZero; } +(CGSize)downloadGIFImageSizeWithRequest:(NSMutableURLRequest*)request { [request setValue:@ "bytes=6-9" forHTTPHeaderField:@ "Range" ]; NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; if (data.length == 4) { short w1 = 0, w2 = 0; [data getBytes:&w1 range:NSMakeRange(0, 1)]; [data getBytes:&w2 range:NSMakeRange(1, 1)]; short w = w1 + (w2 << 8); short h1 = 0, h2 = 0; [data getBytes:&h1 range:NSMakeRange(2, 1)]; [data getBytes:&h2 range:NSMakeRange(3, 1)]; short h = h1 + (h2 << 8); return CGSizeMake(w, h); } return CGSizeZero; } +(CGSize)downloadJPGImageSizeWithRequest:(NSMutableURLRequest*)request { [request setValue:@ "bytes=0-209" forHTTPHeaderField:@ "Range" ]; NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; if ([data length] <= 0x58) { return CGSizeZero; } if ([data length] < 210) { // 肯定只有一个DQT字段 short w1 = 0, w2 = 0; [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)]; [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)]; short w = (w1 << 8) + w2; short h1 = 0, h2 = 0; [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)]; [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)]; short h = (h1 << 8) + h2; return CGSizeMake(w, h); } else { short word = 0x0; [data getBytes:&word range:NSMakeRange(0x15, 0x1)]; if (word == 0xdb) { [data getBytes:&word range:NSMakeRange(0x5a, 0x1)]; if (word == 0xdb) { // 两个DQT字段 short w1 = 0, w2 = 0; [data getBytes:&w1 range:NSMakeRange(0xa5, 0x1)]; [data getBytes:&w2 range:NSMakeRange(0xa6, 0x1)]; short w = (w1 << 8) + w2; short h1 = 0, h2 = 0; [data getBytes:&h1 range:NSMakeRange(0xa3, 0x1)]; [data getBytes:&h2 range:NSMakeRange(0xa4, 0x1)]; short h = (h1 << 8) + h2; return CGSizeMake(w, h); } else { // 一个DQT字段 short w1 = 0, w2 = 0; [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)]; [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)]; short w = (w1 << 8) + w2; short h1 = 0, h2 = 0; [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)]; [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)]; short h = (h1 << 8) + h2; return CGSizeMake(w, h); } } else { return CGSizeZero; } } } |
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://www.cnblogs.com/jingxin1992/p/6150438.html