Ios NSFileManager>;属性FiteMatPath>;图像维度
如何获取保存在文档目录中的图像文件的尺寸 通过使用以下代码,我可以获得其他属性,如NSFileCreationDate和NSFileSizeIos NSFileManager>;属性FiteMatPath>;图像维度,ios,objective-c,nsfilemanager,Ios,Objective C,Nsfilemanager,如何获取保存在文档目录中的图像文件的尺寸 通过使用以下代码,我可以获得其他属性,如NSFileCreationDate和NSFileSize NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:imageFile error:nil]; NSDate *date = (NSDate*)[attributes objectForKey:NSFileCreationDate]; 您不能在
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:imageFile error:nil];
NSDate *date = (NSDate*)[attributes objectForKey:NSFileCreationDate];
您不能在NSFileManager级别执行此操作。您需要读取和解析文件的内容——至少是标题 如何执行此操作取决于文件的类型
最简单的方法是将其作为图像加载,然后查询图像。正如邓肯所说,你不能这样做。但是可以使用
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);
听起来不错。考虑使用
ImageIO
框架。你说什么?