如何检索图像尺寸而不在swift中将CGImage加载到内存中

时间:2016-07-28 14:08:33

标签: objective-c swift uiimage

我想获取图像大小而不加载到内存中,我在目标c中找到但我需要在swift中可以任何人帮助我

    #import <ImageIO/ImageIO.h>

   - (CGSize)sizeOfImageAtURL:(NSURL *)imageURL {
      CGSize imageSize = CGSizeZero;
    CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)imageURL, NULL);
    if (source) {
    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString
   *)kCGImageSourceShouldCache];
 CFDictionaryRef properties = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source, 0, (CFDictionaryRef)options);
    if (properties) {
NSNumber *width = [(NSDictionary *)properties objectForKey:(NSString *)kCGImagePropertyPixelWidth];

   NSNumber *height = [(NSDictionary *)properties objectForKey:(NSString *)kCGImagePropertyPixelHeight];
   if ((width != nil) && (height != nil))
imageSize = CGSizeMake(width.floatValue, height.floatValue);
    CFRelease(properties);
     }
CFRelease(source);
    }
     return imageSize; }

0 个答案:

没有答案