iOS根据图片url获取尺寸
程序员文章站
2022-05-17 22:30:11
可以在UIImage的分类中加入下面的代码,并且引入系统的ImageIO.framework 实践证明,网上的另外一种方法是不可行存在问题的,贴上所示的图片url:http://xs3.op.xywy.com/club.xywy.com/doc/20181017/0fd2a51f9435a4.jpg ......
可以在uiimage的分类中加入下面的代码,并且引入系统的imageio.framework
/** 根据图片的url获取尺寸 @param url url @return cgsize */ + (cgsize)getimagesizewithurl:(id)url; /** * 根据图片url获取图片尺寸 */ + (cgsize)getimagesizewithurl:(id)url{ nsurl * url = nil; if ([url iskindofclass:[nsurl class]]) { url = url; } if ([url iskindofclass:[nsstring class]]) { url = [nsurl urlwithstring:url]; } if (!url) { return cgsizezero; } cgimagesourceref imagesourceref = cgimagesourcecreatewithurl((cfurlref)url, null); cgfloat width = 0, height = 0; if (imagesourceref) { cfdictionaryref imageproperties = cgimagesourcecopypropertiesatindex(imagesourceref, 0, null); if (imageproperties != null) { cfnumberref widthnumberref = cfdictionarygetvalue(imageproperties, kcgimagepropertypixelwidth); if (widthnumberref != null) { cfnumbergetvalue(widthnumberref, kcfnumberfloat64type, &width); } cfnumberref heightnumberref = cfdictionarygetvalue(imageproperties, kcgimagepropertypixelheight); if (heightnumberref != null) { cfnumbergetvalue(heightnumberref, kcfnumberfloat64type, &height); } cfrelease(imageproperties); } cfrelease(imagesourceref); } return cgsizemake(width, height); }
实践证明,网上的另外一种方法是不可行存在问题的,贴上所示的图片url:http://xs3.op.xywy.com/club.xywy.com/doc/20181017/0fd2a51f9435a4.jpg
// 根据图片url获取图片尺寸 -(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 if([pathextendsion isequal:@"gif"]) { size = [self getgifimagesizewithrequest: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; } // 获取png图片的大小 -(cgsize)getpngimagesizewithrequest:(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; } // 获取gif图片的大小 -(cgsize)getgifimagesizewithrequest:(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; } // 获取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] < 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; } } }
源自:https://www.jianshu.com/p/9984c37f3f54