IOS开发之网络图片处理

  //图片压缩

UIImage* image=[UIImage imageWithData:data];

NSData *data1 = UIImageJPEGRepresentation(image, 0.3);

//图片设配问题,在用imageView后调用,可定制高度,不会产生图片变形问题
 [imageView setClipsToBounds:YES];
[imageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
//网络获取网络图片的宽高
//调用方法为 CGSize sizeImage= [self getImageSizeWithURL:model.imgUrl]; 注意:这是类方法
+(CGSize)getImageSizeWithURL:(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; // url不正确返回CGSizeZero NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
NSString* pathExtendsion = [URL.pathExtension lowercaseString]; CGSize size = CGSizeZero;
if([pathExtendsion isEqualToString:@"png"]){
size=[self getPNGImageSizeWithRequest:request];
} else{
size = [self getJPGImageSizeWithRequest:request];
}
if(CGSizeEqualToSize(CGSizeZero, size)) // 如果获取文件头信息失败,发送异步请求请求原图
{
NSData* data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:URL] returningResponse:nil error:nil]; UIImage* image = [UIImage imageWithData:data];
if(image)
{
size = image.size;
}
}
return size;
}
+(CGSize)getPNGImageSizeWithRequest:(NSMutableURLRequest*)request
{
[request setValue:@"bytes=16-23" forHTTPHeaderField:@"Range"];
// NSURLRequest
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; if(data.length == )
{
int w1 = , w2 = , w3 = , w4 = ;
[data getBytes:&w1 range:NSMakeRange(, )];
[data getBytes:&w2 range:NSMakeRange(, )];
[data getBytes:&w3 range:NSMakeRange(, )];
[data getBytes:&w4 range:NSMakeRange(, )];
int w = (w1 << ) + (w2 << ) + (w3 << ) + w4;
int h1 = , h2 = , h3 = , h4 = ;
[data getBytes:&h1 range:NSMakeRange(, )];
[data getBytes:&h2 range:NSMakeRange(, )];
[data getBytes:&h3 range:NSMakeRange(, )];
[data getBytes:&h4 range:NSMakeRange(, )];
int h = (h1 << ) + (h2 << ) + (h3 << ) + h4;
return CGSizeMake(w, h);
}
return CGSizeZero;
}
// 获取gif图片的大小
+(CGSize)getGIFImageSizeWithRequest:(NSMutableURLRequest*)request
{
[request setValue:@"bytes=6-9" forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if(data.length == )
{
short w1 = , w2 = ;
[data getBytes:&w1 range:NSMakeRange(, )];
[data getBytes:&w2 range:NSMakeRange(, )];
short w = w1 + (w2 << );
short h1 = , h2 = ;
[data getBytes:&h1 range:NSMakeRange(, )];
[data getBytes:&h2 range:NSMakeRange(, )];
short h = h1 + (h2 << );
return CGSizeMake(w, h);
}
return CGSizeZero;
}
// 获取jpg图片的大小
+(CGSize)getJPGImageSizeWithRequest:(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] < ) {// 肯定只有一个DQT字段
short w1 = , w2 = ;
[data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
[data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
short w = (w1 << ) + w2;
short h1 = , h2 = ;
[data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
[data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
short h = (h1 << ) + 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 = , w2 = ;
[data getBytes:&w1 range:NSMakeRange(0xa5, 0x1)];
[data getBytes:&w2 range:NSMakeRange(0xa6, 0x1)];
short w = (w1 << ) + w2;
short h1 = , h2 = ;
[data getBytes:&h1 range:NSMakeRange(0xa3, 0x1)];
[data getBytes:&h2 range:NSMakeRange(0xa4, 0x1)];
short h = (h1 << ) + h2;
return CGSizeMake(w, h);
} else {// 一个DQT字段
short w1 = , w2 = ;
[data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
[data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
short w = (w1 << ) + w2;
short h1 = , h2 = ;
[data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
[data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
short h = (h1 << ) + h2;
return CGSizeMake(w, h);
}
} else {
return CGSizeZero;
}
}
}

为View加阴影

view.layer.masksToBounds = NO;

view.layer.shadowOffset = CGSizeMake(0, 1.9);

view.layer.shadowRadius = 3;

view.layer.shadowOpacity = 0.14;

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport

//适配最新系统

上一篇:python 2017.1.9


下一篇:深入Ambari Metrics 机制分析