UIImage+itdCategory.h
#import <UIKit/UIKit.h>
typedef void(^ImageSizeBlock)(CGSize size);
@interface UIImage (itdCategory)
/**
* 获取网络url图片的具体大小
*/
+ (CGSize)itd_sizeOfImageWithUrlStr:(NSString *)imgUrlStr;
/**
* 获取网络url图片的具体大小
*/
+ (void)itd_sizeOfImageWithUrlStr:(NSString *)imgUrlStr sizeGetDo:(ImageSizeBlock)doBlock;
@end
UIImage+itdCategory.m
#import "UIImage+itdCategory.h"
NSString *const kPngRangeValue = @"bytes=16-23";
NSString *const kJpgRangeValue = @"bytes=0-209";
NSString *const kGifRangeValue = @"bytes=6-9";
@implementation UIImage (itdCategory)
+ (CGSize)itd_sizeOfImageWithUrlStr:(NSString *)imgUrlStr{
if ([imgUrlStr hasSuffix:@".png"]) {
return [self size_downloadPNGImageWithUrlString:imgUrlStr];
}else if ([imgUrlStr hasSuffix:@".gif"])
{
return [self size_downloadGIFImageWithUrlString:imgUrlStr];
}else{
return [self size_downloadJPGImageWithUrlString:imgUrlStr];
}
return CGSizeZero;
}
+ (void)itd_sizeOfImageWithUrlStr:(NSString *)imgUrlStr sizeGetDo:(ImageSizeBlock)doBlock
{
NSString *extensionStr = [[imgUrlStr pathExtension] lowercaseString];
CGSize imgSize = CGSizeZero;
if ([extensionStr isEqualToString:@"png"]) {
imgSize = [UIImage size_downloadPNGImageWithUrlString:imgUrlStr];
}else if ([extensionStr isEqualToString:@"gif"])
{
imgSize = [UIImage size_downloadGIFImageWithUrlString:imgUrlStr];
}else{
imgSize = [UIImage size_downloadJPGImageWithUrlString:imgUrlStr];
}
if (doBlock) {
doBlock(imgSize);
}
return ;
}
+ (CGSize)size_downloadJPGImageWithUrlString:(NSString *)urlString{
NSString *URLString = urlString;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URLString]];
[request setValue:kJpgRangeValue forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if (data == nil || [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;
}
}
}
+ (CGSize)size_downloadPNGImageWithUrlString:(NSString *)urlString{
NSString *URLString = urlString;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URLString]];
[request setValue:kPngRangeValue forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if(data && 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)size_downloadGIFImageWithUrlString:(NSString *)urlString{
NSString *URLString = urlString;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URLString]];
[request setValue:kGifRangeValue forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if(data && data.length == 4)
{
short w1 = 0, w2 = 0;
[data getBytes:&w1 range:NSMakeRange(1, 1)];
[data getBytes:&w2 range:NSMakeRange(0, 1)];
short w = (w1 << 8) + w2;
short h1 = 0, h2 = 0;
[data getBytes:&h1 range:NSMakeRange(3, 1)];
[data getBytes:&h2 range:NSMakeRange(2, 1)];
short h = (h1 << 8) + h2;
return CGSizeMake(w, h);
}
return CGSizeZero;
}
@end
获取网络图片尺寸大小
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、介绍 作为ios程序开发员,有时候难免需要获取网络图片的真实宽和高,现在网上获取图片尺寸的方法各种各样,但是大...
- 为按钮设置网络图片, 可以使用SDWebImage第三方库提供的UIButton+WebCache.h 分类 提供...
- NSURL *url = [NSURL URLWithString:[model.content objectFo...
- NSURL *url = [NSURL URLWithString:[model.content objectFo...