图片围绕中心截取显示
系统返回的图片的大小和需要显示的ImageView差别过大,直接显示导致图片严重变形,这时候需要对图片进行截取
1.首先使用SD下载图片 直接上代码 粘贴就可以使用
1.1创建工具类 Singleton.h 直接粘贴即可
#define singleton_interface(className) \ + (className *)shared##className; #define singleton_implementation(class) \ static class *_instance; \ \ + (id)allocWithZone:(struct _NSZone *)zone \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [super allocWithZone:zone]; \ }); \ \ return _instance; \ } \ \ + (instancetype)shared##class \ { \ if (_instance == nil) { \ _instance = [[class alloc] init]; \ } \ \ return _instance; \ }
1.2 创建一个继承与NSObject的类
#import <Foundation/Foundation.h> #import "Singleton.h" @interface Single : NSObject singleton_interface(Single) +(CGSize)downloadImageSizeWithURL:(id)imageURL; @end
#import "Single.h" #import "SDImageCache.h" @implementation Single singleton_implementation(Single) +(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; NSString* absoluteString = URL.absoluteString; #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]; NSString* pathExtendsion = [URL.pathExtension lowercaseString]; CGSize size = CGSizeZero; if([pathExtendsion isEqualToString:@"png"]){ size = [self downloadPNGImageSizeWithRequest:request]; } else if([pathExtendsion isEqual:@"gif"]) { size = [self downloadGIFImageSizeWithRequest:request]; } else{ size = [self downloadJPGImageSizeWithRequest:request]; } if(CGSizeEqualToSize(CGSizeZero, size)) { NSData* data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:URL] returningResponse:nil error:nil]; UIImage* image = [UIImage imageWithData:data]; if(image) { #ifdef dispatch_main_sync_safe [[SDImageCache sharedImageCache] storeImage:image recalculateFromImage:YES imageData:data forKey:URL.absoluteString toDisk:YES]; #endif size = image.size; } } 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; } } } @end
2,封装一个使用的方法,可以放在工具类里面 也可以写在当前类
+ (CGRect)cutImage:(NSURL *)imageUrl WithWide:(CGFloat)imageWide height:(CGFloat)imageHeight{ CGSize aWidth=[Single downloadImageSizeWithURL:imageUrl]; float width=aWidth.width; float height=aWidth.height; float imgwidth; float imgheight; float scales=width/height; if (width/height>imageWide/imageHeight) { imgheight=imageHeight; imgwidth=imageHeight*scales; }else { imgwidth=imageWide; imgheight=imageWide/scales; } CGRect rect = CGRectMake(0, 0, imgwidth, imgheight); return rect; }
3,使用方法
//imageUrl 需要截取的路径;
//imageWide 显示image的iamgeView的宽 ;
//imageHeight 显示image的iamgeview的高 ;
注意1.需要设置显示imageview的center
2.需要clipsToBounds
UIImageView *topImageViw = [[UIImageView alloc] initWithFrame:CGRectMake(i*WINDOW_WIDTH, 0, WINDOW_WIDTH, scrollView.frame.size.height)]; topImageViw.bounds=[TncTool cutImage:[NSURL URLWithString:self.iamgeUrl] WithWide:WINDOW_WIDTH height:scrollView.frame.size.height]; topImageViw.center = CGPointMake(WINDOW_WIDTH/2, scrollView.frame.size.height/2); topImageViw.clipsToBounds = YES; [topImageViw sd_setImageWithURL:[NSURL URLWithString:self.oriArr[i]] placeholderImage:[UIImage imageNamed:PLACEHOLDER]]; [scrollView addSubview:topImageViw];
至此,凑合着可以使用了.....