您好,登錄后才能下訂單哦!
NSURL *url = [NSURL URLWithString:[model.content objectForKey:@"p_w_picpath"]]; //請求網絡地址數據的同步方法 //因為這個方法在子線程(全局隊列)中執行,所以不需要考慮死線程的問題 SDWebImageManager *manager = [SDWebImageManager sharedManager]; [manager diskImageExistsForURL:url]; if ([manager diskImageExistsForURL:url]) { LOG(@"11111") self.speakImage = [[manager p_w_picpathCache] p_w_picpathFromDiskCacheForKey:url.absoluteString]; }else{ LOG(@"3333333") NSData *data = [NSData dataWithContentsOfURL:url]; self.speakImage = [UIImage p_w_picpathWithData:data]; }
/**
獲取網絡圖片的Size, 先通過文件頭來獲取圖片大小
如果失敗 會下載完整的圖片Data 來計算大小 所以最好別放在主線程
如果你有使用SDWebImage就會先看下 SDWebImage有緩存過改圖片沒有
支持文件頭大小的格式 png、gif、jpg http://www.cocoachina.com/bbs/read.php?tid=165823
*/
+(CGSize)downloadImageSizeWithURL:(id)p_w_picpathURL;
//討厭警告 -(id)diskImageDataBySearchingAllPathsForKey:(id)key{return nil;} +(CGSize)downloadImageSizeWithURL:(id)p_w_picpathURL { NSURL* URL = nil; if([p_w_picpathURL isKindOfClass:[NSURL class]]){ URL = p_w_picpathURL; } if([p_w_picpathURL isKindOfClass:[NSString class]]){ URL = [NSURL URLWithString:p_w_picpathURL]; } if(URL == nil) return CGSizeZero; NSString* absoluteString = URL.absoluteString; #ifdef dispatch_main_sync_safe if([[SDImageCache sharedImageCache] diskImageExistsWithKey:absoluteString]) { UIImage* p_w_picpath = [[SDImageCache sharedImageCache] p_w_picpathFromMemoryCacheForKey:absoluteString]; if(!p_w_picpath) { NSData* data = [[SDImageCache sharedImageCache] performSelector:@selector(diskImageDataBySearchingAllPathsForKey:) withObject:URL.absoluteString]; p_w_picpath = [UIImage p_w_picpathWithData:data]; } if(!p_w_picpath) { return p_w_picpath.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* p_w_picpath = [UIImage p_w_picpathWithData:data]; if(p_w_picpath) { #ifdef dispatch_main_sync_safe [[SDImageCache sharedImageCache] storeImage:p_w_picpath recalculateFromImage:YES p_w_picpathData:data forKey:URL.absoluteString toDisk:YES]; #endif size = p_w_picpath.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 h2 = 0, h3 = 0, h4 = 0, h5 = 0; [data getBytes:&h2; range:NSMakeRange(4, 1)]; [data getBytes:&h3; range:NSMakeRange(5, 1)]; [data getBytes:&h4; range:NSMakeRange(6, 1)]; [data getBytes:&h5; range:NSMakeRange(7, 1)]; int h = (h2 << 24) + (h3 << 16) + (h4 << 8) + h5; 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 h2 = 0, h3 = 0; [data getBytes:&h2; range:NSMakeRange(2, 1)]; [data getBytes:&h3; range:NSMakeRange(3, 1)]; short h = h2 + (h3 << 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 h2 = 0, h3 = 0; [data getBytes:&h2; range:NSMakeRange(0x5e, 0x1)]; [data getBytes:&h3; range:NSMakeRange(0x5f, 0x1)]; short h = (h2 << 8) + h3; 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 h2 = 0, h3 = 0; [data getBytes:&h2; range:NSMakeRange(0xa3, 0x1)]; [data getBytes:&h3; range:NSMakeRange(0xa4, 0x1)]; short h = (h2 << 8) + h3; 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 h2 = 0, h3 = 0; [data getBytes:&h2; range:NSMakeRange(0x5e, 0x1)]; [data getBytes:&h3; range:NSMakeRange(0x5f, 0x1)]; short h = (h2 << 8) + h3; return CGSizeMake(w, h); } } else { return CGSizeZero; } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。