IOS gets the network image size, ios gets the image size
During iOS development, you often need to attach images through network requests. Sometimes, you need to know the image size in advance before creating a UIImageView or UIButton to display images, create a widget of the corresponding size based on the image size. However, for network images, it is a little difficult to obtain the size through the optimal method. The general idea is as follows:
If SDWebImage is used, first check whether the image has been cached. If not, first obtain the image size through the file header (for png, gif, and jpg files, obtain the size ), if acquisition fails, download the complete image data and calculate the size. If SDWebImage is used, use SDWebImage to cache the image.
+ (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;
# Ifdef dispatch_main_sync_safe
If ([[SDImageCache nvidimagecache] diskImageExistsWithKey: absoluteString])
{
UIImage * image = [[SDImageCache nvidimagecache] imageFromMemoryCacheForKey: absoluteString];
If (! Image)
{
NSData * data = [[SDImageCache nvidimagecache] performSelector: @ selector (diskImageDataBySearchingAllPathsForKey :) withObject: URL. absoluteString];
Image = [UIImage imageWithData: data];
}
If (image)
{
Return image. size;
}
}
# Endif
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL: URL cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 1];
NSString * pathExtendsion = [URL. pathExtension lowercaseString];
CGSize size = CGSizeZero;
If ([pathExtendsion rangeOfString: @ "png"]. location! = NSNotFound ){
Size = [self downloadPNGImageSizeWithRequest: request];
}
Else if ([pathExtendsion rangeOfString: @ "gif"]. location! = NSNotFound)
{
Size = [self downloadGIFImageSizeWithRequest: request];
}
Else {
Size = [self downloadJPGImageSizeWithRequest: request];
}
If (CGSizeEqualToSize (CGSizeZero, size ))
{
NSData * data = [NSData dataWithContentsOfURL: URL];
UIImage * image = [UIImage imageWithData: data];
If (image)
{
// If SDWebImage is not used, ignore it; cache the image
# Ifdef dispatch_main_sync_safe
[[SDImageCache nvidimagecache] storeImage: image recalculateFromImage: YES imageData: data forKey: URL. absoluteString toDisk: YES];
# Endif
Size = image. size;
}
}
// Filter out non-conforming images. The big picture is too big to waste traffic and the user experience is poor.
If (size. height> 2048 | size. height <= 0 | size. width> 2048 | size. width <= 0 ){
Return CGSizeZero;
}
Else
{
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) {// there must be only one DQT Field
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) {// two DQT Fields
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 {// a dqt Field
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;
}
}
}