我从 Apple, Inc. 的名为ImageApp的示例项目中提取了以下代码。因此,您需要该项目来仔细查看下面的代码。键值绑定到mTree。
// AppDelegate.h
@interface AppDelegate : NSObject {
IBOutlet NSTreeController *mTree;
NSURL *mUrl;
}
// AppDelegate.m
- (void)setPictureInfo:(NSString *)filepath {
NSURL *url = [[NSURL alloc] init];
url = [self convertpathURL:filepath:NO]; // Converting a file path to a url
[self getImageInfo:url];
}
- (void)getImageInfo:(NSURL *)url {
if (nil == url) {
return;
}
if ([url isEqual:mUrl])
return;
mUrl = url;
CGImageSourceRef source = NULL;
if (url) source = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
// CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
if (source) {
// get image properties (height, width, depth, metadata etc.) for display
NSDictionary *props = (__bridge_transfer NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
[mTree setContent:[self propTree:props]];
}
else { // couldn't make image source for image, so display nothing
[mTree setContent:nil];
}
}
static NSString *ImageIOLocalizedString (NSString *key) {
static NSBundle *b = nil;
if (b == nil)
b = [NSBundle bundleWithIdentifier:@"com.apple.ImageIO.framework"];
// Returns a localized version of the string designated by 'key' in table 'CGImageSource'.
return [b localizedStringForKey:key value:key table: @"CGImageSource"];
}
- (NSArray *)propTree:(NSDictionary *)branch {
NSMutableArray *tree = [[NSMutableArray alloc] init];
for (NSInteger i3 = 0; i3 < [branch count]; i3++) {
NSArray *keys = [[branch allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSString *key = [keys objectAtIndex:i3];
NSString *locKey = ImageIOLocalizedString(key);
id obj = [branch objectForKey:key];
NSDictionary* leaf = nil;
if ([obj isKindOfClass:[NSDictionary class]])
leaf = [NSDictionary dictionaryWithObjectsAndKeys:
locKey,@"key", @"",@"val", [self propTree:obj],@"children", nil];
else
leaf = [NSDictionary dictionaryWithObjectsAndKeys:
locKey,@"key", obj,@"val", nil];
[tree addObject:leaf];
}
return tree;
}