ios - How can I get the dimensions of an image without downloading the entire image? -
so far have method:
+ (cgsize) imagedimensions: (nsstring *)url { nsurl *imagefileurl = [nsurl urlwithstring: url]; cgimagesourceref imagesource = cgimagesourcecreatewithurl((__bridge cfurlref)imagefileurl, null); if (imagesource == null) { return cgsizemake(0, 0); } cgfloat width = 0.0f, height = 0.0f; cfdictionaryref imageproperties = cgimagesourcecopypropertiesatindex(imagesource, 0, null); if (imageproperties != null) { cfnumberref widthnum = cfdictionarygetvalue(imageproperties, kcgimagepropertypixelwidth); if (widthnum != null) { cfnumbergetvalue(widthnum, kcfnumberfloattype, &width); } cfnumberref heightnum = cfdictionarygetvalue(imageproperties, kcgimagepropertypixelheight); if (heightnum != null) { cfnumbergetvalue(heightnum, kcfnumberfloattype, &height); } cfrelease(imageproperties); } return cgsizemake(width, height); }
however doing tests , seems method runs fast if download entire image , check size property. had thought method worked downloading image's meta data or headers , checking resolution there. wrong? there quick way image dimensions internet image in objective c quick possible without grabbing image?
most graphical formats have dimension stored in header. if limit subset of formats (png/jpg/gif, example), , implement own header parsing code, it's possible. you'll on own, though - core graphics image apis not work partial files.
to request part of file 'net, can use range
header. web servers might not honor it, though. request number of bytes that's maximum among header sizes of supported formats. also, might want validate magic bytes in header.
Comments
Post a Comment