• <i id='apCiG'><tr id='apCiG'><dt id='apCiG'><q id='apCiG'><span id='apCiG'><b id='apCiG'><form id='apCiG'><ins id='apCiG'></ins><ul id='apCiG'></ul><sub id='apCiG'></sub></form><legend id='apCiG'></legend><bdo id='apCiG'><pre id='apCiG'><center id='apCiG'></center></pre></bdo></b><th id='apCiG'></th></span></q></dt></tr></i><div id='apCiG'><tfoot id='apCiG'></tfoot><dl id='apCiG'><fieldset id='apCiG'></fieldset></dl></div>
      <bdo id='apCiG'></bdo><ul id='apCiG'></ul>

    <tfoot id='apCiG'></tfoot>

  • <small id='apCiG'></small><noframes id='apCiG'>

    1. <legend id='apCiG'><style id='apCiG'><dir id='apCiG'><q id='apCiG'></q></dir></style></legend>

        无需下载的远程图像大小

        时间:2023-10-03
          <i id='TYI2s'><tr id='TYI2s'><dt id='TYI2s'><q id='TYI2s'><span id='TYI2s'><b id='TYI2s'><form id='TYI2s'><ins id='TYI2s'></ins><ul id='TYI2s'></ul><sub id='TYI2s'></sub></form><legend id='TYI2s'></legend><bdo id='TYI2s'><pre id='TYI2s'><center id='TYI2s'></center></pre></bdo></b><th id='TYI2s'></th></span></q></dt></tr></i><div id='TYI2s'><tfoot id='TYI2s'></tfoot><dl id='TYI2s'><fieldset id='TYI2s'></fieldset></dl></div>
            <tbody id='TYI2s'></tbody>
        • <legend id='TYI2s'><style id='TYI2s'><dir id='TYI2s'><q id='TYI2s'></q></dir></style></legend>
        • <small id='TYI2s'></small><noframes id='TYI2s'>

            <bdo id='TYI2s'></bdo><ul id='TYI2s'></ul>

                  <tfoot id='TYI2s'></tfoot>
                1. 本文介绍了无需下载的远程图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 Obj-c/Cocoa-touch,我正在尝试获取图像列表的图像大小,但我不想下载它们.有什么简单的方法吗?我找到了一些其他语言的解决方案,例如 this SO question 但是我正在寻找一种更简单的方法.

                  Using Obj-c / Cocoa-touch, I'm trying to get the image size of a list of images but I don't want to download them. Is there some easy way to do it? I found some solutions in other languages like this SO question but I'm looking for a easier way.

                  谢谢

                  推荐答案

                  这是我用于此的 UIImage 类别.它基于 fastimage.一个主要警告是 NSURLConnectionDelegate 设置为 NSURL 本身,这可能会在某些情况下导致冲突.这还没有完成(例如文件 URL 被忽略...),但您可以看到它的去向.

                  Here is a UIImage category I use for this. It is based on fastimage. One major warning is the NSURLConnectionDelegate is set to the NSURL itself and this may cause clashes in certain situations. This isn't complete yet (file URL's are ignored for example...) but you can see where it is going.

                  标题:

                  #import <UIKit/UIKit.h>
                  
                  typedef void (^UIImageSizeRequestCompleted) (NSURL* imgURL, CGSize size);
                  
                  @interface UIImage (RemoteSize)
                  
                  + (void) requestSizeFor: (NSURL*) imgURL completion: (UIImageSizeRequestCompleted) completion;
                  
                  @end
                  

                  来源:

                  #import "UIImage+RemoteSize.h"
                  
                  #import <objc/runtime.h>
                  
                  static char *kSizeRequestDataKey = "NSURL.sizeRequestData";
                  static char *kSizeRequestTypeKey = "NSURL.sizeRequestType";
                  static char *kSizeRequestCompletionKey = "NSURL.sizeRequestCompletion";
                  
                  typedef uint32_t dword;
                  
                  @interface NSURL (RemoteSize)
                  @property (nonatomic, strong) NSMutableData* sizeRequestData;
                  @property (nonatomic, strong) NSString* sizeRequestType;
                  @property (nonatomic, copy) UIImageSizeRequestCompleted sizeRequestCompletion;
                  @end
                  
                  @implementation NSURL (RemoteSize)
                  
                  - (void) setSizeRequestCompletion: (UIImageSizeRequestCompleted) block {
                      objc_setAssociatedObject(self, &kSizeRequestCompletionKey, block, OBJC_ASSOCIATION_COPY);
                  }
                  
                  - (UIImageSizeRequestCompleted) sizeRequestCompletion {
                      return objc_getAssociatedObject(self, &kSizeRequestCompletionKey);
                  }
                  
                  - (void) setSizeRequestData:(NSMutableData *)sizeRequestData {
                      objc_setAssociatedObject(self, &kSizeRequestDataKey, sizeRequestData, OBJC_ASSOCIATION_RETAIN);
                  }
                  
                  - (NSMutableData*) sizeRequestData {
                      return objc_getAssociatedObject(self, &kSizeRequestDataKey);
                  }
                  
                  - (void) setSizeRequestType:(NSString *)sizeRequestType {
                      objc_setAssociatedObject(self, &kSizeRequestTypeKey, sizeRequestType, OBJC_ASSOCIATION_RETAIN);
                  }
                  
                  - (NSString*) sizeRequestType {
                      return objc_getAssociatedObject(self, &kSizeRequestTypeKey);
                  }
                  
                  #pragma mark - NSURLConnectionDelegate
                  - (void) connection: (NSURLConnection*) connection didReceiveResponse:(NSURLResponse *)response {
                      [self.sizeRequestData setLength: 0];    //Redirected => reset data
                  }
                  
                  - (void) connection: (NSURLConnection*) connection didReceiveData:(NSData *)data {
                      NSMutableData* receivedData = self.sizeRequestData;
                  
                      if( !receivedData ) {
                          receivedData = [NSMutableData data];
                          self.sizeRequestData = receivedData;
                      }
                  
                      [receivedData appendData: data];
                  
                      //Parse metadata
                      const unsigned char* cString = [receivedData bytes];
                      const NSInteger length = [receivedData length];
                  
                      const char pngSignature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
                      const char bmpSignature[2] = {66, 77};
                      const char gifSignature[2] = {71, 73}; 
                      const char jpgSignature[2] = {255, 216};
                  
                      if(!self.sizeRequestType ) {
                          if( memcmp(pngSignature, cString, 8) == 0 ) {
                              self.sizeRequestType = @"PNG";
                          }
                          else if( memcmp(bmpSignature, cString, 2) == 0 ) {
                              self.sizeRequestType = @"BMP";
                          }
                          else if( memcmp(jpgSignature, cString, 2) == 0 ) {
                              self.sizeRequestType = @"JPG";
                          }
                          else if( memcmp(gifSignature, cString, 2) == 0 ) {
                              self.sizeRequestType = @"GIF";
                          }
                      }
                  
                      if( [self.sizeRequestType isEqualToString: @"PNG"] ) {
                          char type[5];
                          int offset = 8;
                  
                          dword chunkSize = 0;
                          int chunkSizeSize = sizeof(chunkSize);
                  
                          if( offset+chunkSizeSize > length ) 
                              return;
                  
                          memcpy(&chunkSize, cString+offset, chunkSizeSize);
                          chunkSize = OSSwapInt32(chunkSize);
                          offset += chunkSizeSize;
                  
                          if( offset + chunkSize > length ) 
                              return;
                  
                          memcpy(&type, cString+offset, 4); type[4]='';
                          offset += 4;
                  
                          if( strcmp(type, "IHDR") == 0 ) {   //Should always be first
                              dword width = 0, height = 0;
                              memcpy(&width, cString+offset, 4);
                              offset += 4;
                              width = OSSwapInt32(width);
                  
                              memcpy(&height, cString+offset, 4);
                              offset += 4;
                              height = OSSwapInt32(height);
                  
                              if( self.sizeRequestCompletion ) {
                                  self.sizeRequestCompletion(self, CGSizeMake(width, height));
                              }
                  
                              self.sizeRequestCompletion = nil;
                  
                              [connection cancel];
                          }
                      }
                      else if( [self.sizeRequestType isEqualToString: @"BMP"] ) {
                          int offset = 18;
                          dword width = 0, height = 0;
                          memcpy(&width, cString+offset, 4);
                          offset += 4;
                  
                          memcpy(&height, cString+offset, 4);
                          offset += 4;
                  
                          if( self.sizeRequestCompletion ) {
                              self.sizeRequestCompletion(self, CGSizeMake(width, height));
                          }
                  
                          self.sizeRequestCompletion = nil;
                  
                          [connection cancel];
                      }
                      else if( [self.sizeRequestType isEqualToString: @"JPG"] ) {
                          int offset = 4;
                          dword block_length = cString[offset]*256 + cString[offset+1];
                  
                          while (offset<length) {
                              offset += block_length;
                  
                              if( offset >= length )
                                  break;
                              if( cString[offset] != 0xFF )
                                  break;
                              if( cString[offset+1] == 0xC0 ||
                                  cString[offset+1] == 0xC1 ||
                                  cString[offset+1] == 0xC2 ||
                                  cString[offset+1] == 0xC3 ||
                                  cString[offset+1] == 0xC5 ||
                                  cString[offset+1] == 0xC6 ||
                                  cString[offset+1] == 0xC7 ||
                                  cString[offset+1] == 0xC9 ||
                                  cString[offset+1] == 0xCA ||
                                  cString[offset+1] == 0xCB ||
                                  cString[offset+1] == 0xCD ||
                                  cString[offset+1] == 0xCE ||
                                  cString[offset+1] == 0xCF ) {
                  
                                  dword width = 0, height = 0;
                  
                                  height = cString[offset+5]*256 + cString[offset+6];
                                  width = cString[offset+7]*256 + cString[offset+8];
                  
                                  if( self.sizeRequestCompletion ) {
                                      self.sizeRequestCompletion(self, CGSizeMake(width, height));
                                  }
                  
                                  self.sizeRequestCompletion = nil;
                  
                                  [connection cancel];
                  
                              }
                              else {
                                  offset += 2;
                                  block_length = cString[offset]*256 + cString[offset+1];
                              }
                  
                          }
                      }
                      else if( [self.sizeRequestType isEqualToString: @"GIF"] ) {
                          int offset = 6;
                          dword width = 0, height = 0;
                          memcpy(&width, cString+offset, 2);
                          offset += 2;
                  
                          memcpy(&height, cString+offset, 2);
                          offset += 2;
                  
                          if( self.sizeRequestCompletion ) {
                              self.sizeRequestCompletion(self, CGSizeMake(width, height));
                          }
                  
                          self.sizeRequestCompletion = nil;
                  
                          [connection cancel];
                      }
                  }
                  
                  - (void) connection: (NSURLConnection*) connection didFailWithError:(NSError *)error {
                      if( self.sizeRequestCompletion ) 
                          self.sizeRequestCompletion(self, CGSizeZero);
                  }
                  
                  - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
                      return cachedResponse;
                  }
                  
                  - (void) connectionDidFinishLoading: (NSURLConnection *)connection {
                      // Basically, we failed to obtain the image size using metadata and the
                      // entire image was downloaded...
                  
                      if(!self.sizeRequestData.length) {
                          self.sizeRequestData = nil;
                      }
                      else {
                          //Try parse to UIImage
                          UIImage* image = [UIImage imageWithData: self.sizeRequestData];
                  
                          if( self.sizeRequestCompletion && image) {
                              self.sizeRequestCompletion(self, [image size]);
                              return;
                          }
                      }
                  
                      self.sizeRequestCompletion(self, CGSizeZero);
                  }
                  
                  @end
                  
                  @implementation UIImage (RemoteSize)
                  
                  + (void) requestSizeFor: (NSURL*) imgURL completion: (UIImageSizeRequestCompleted) completion {
                  
                      if( [imgURL isFileURL] ) {
                          //Load from file stream
                      }
                      else {
                          imgURL.sizeRequestCompletion = completion;
                  
                          NSURLRequest* request = [NSURLRequest requestWithURL: imgURL];
                          NSURLConnection* conn = [NSURLConnection connectionWithRequest: request delegate: imgURL];
                          [conn scheduleInRunLoop: [NSRunLoop mainRunLoop] forMode: NSDefaultRunLoopMode];
                          [conn start];
                      }
                  }
                  
                  @end
                  

                  这篇关于无需下载的远程图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:iOS4 创建后台定时器 下一篇:使用 iOS4 在后台下载数据

                  相关文章

                  <legend id='40Xxi'><style id='40Xxi'><dir id='40Xxi'><q id='40Xxi'></q></dir></style></legend>
                  <i id='40Xxi'><tr id='40Xxi'><dt id='40Xxi'><q id='40Xxi'><span id='40Xxi'><b id='40Xxi'><form id='40Xxi'><ins id='40Xxi'></ins><ul id='40Xxi'></ul><sub id='40Xxi'></sub></form><legend id='40Xxi'></legend><bdo id='40Xxi'><pre id='40Xxi'><center id='40Xxi'></center></pre></bdo></b><th id='40Xxi'></th></span></q></dt></tr></i><div id='40Xxi'><tfoot id='40Xxi'></tfoot><dl id='40Xxi'><fieldset id='40Xxi'></fieldset></dl></div>
                  1. <small id='40Xxi'></small><noframes id='40Xxi'>

                        <bdo id='40Xxi'></bdo><ul id='40Xxi'></ul>
                      <tfoot id='40Xxi'></tfoot>