【问题标题】:NSFileManager > attributesOfItemAtPath > Image DimensionNSFileManager > attributesOfItemAtPath > Image Dimension
【发布时间】:2014-01-14 11:41:06
【问题描述】:

如何获取文档目录中保存的图像文件的尺寸?

我可以使用以下代码获取其他属性,例如 NSFileCreationDate 和 NSFileSize

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:imageFile error:nil];
NSDate *date = (NSDate*)[attributes objectForKey:NSFileCreationDate];

【问题讨论】:

    标签: ios objective-c nsfilemanager


    【解决方案1】:

    正如邓肯所说,你不能这样做。但是可以使用ImageIO 框架。请尝试以下代码。

    NSURL *imageFileURL = [NSURL fileURLWithPath:...]; // Put your image in a URL
    CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
    if (imageSource == NULL) {
    // Error loading image
    ...
    return;
    }
    
    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, kCFNumberCGFloatType, &width);
    }
    
    CFNumberRef heightNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
    if (heightNum != NULL) {
        CFNumberGetValue(heightNum, kCFNumberCGFloatType, &height);
    }
    
    CFRelease(imageProperties);
    }
    
    NSLog(@"Image dimensions: %.0f x %.0f px", width, height);
    

    【讨论】:

      【解决方案2】:

      你不能在 NSFileManager 级别。您需要读取和解析文件的内容 - 至少是标题。

      您如何执行此操作取决于文件的类型。

      最简单的做法是将其加载为图像,然后查询图像。

      【讨论】:

      • 听起来不错。考虑使用ImageIO 框架。你说什么?
      猜你喜欢
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2014-12-13
      相关资源
      最近更新 更多