在 Objective-C 中,有一个 Photos Framework a.k.a. PhotoKit这使 iOS 开发人员能够访问 iPhone 和 iPad 上的照片库,并检索图片/视频及其元数据。
Mac 开发人员将如何执行类似的任务? PhotoKit 似乎只能在 iOS 8.0 中使用。是否有适用于 Mac OS X 的 Photos Framework 的等价物?
最佳答案
Media Library Framework是去的地方。
用法:
@import MediaLibrary;
- (void) awakeFromNib
{
NSDictionary *options = @{
MLMediaLoadSourceTypesKey: @(MLMediaSourceTypeImage),
MLMediaLoadIncludeSourcesKey: @[MLMediaSourcePhotosIdentifier]
};
MLMediaLibrary *mediaLibrary = [[MLMediaLibrary alloc] initWithOptions:options];
self.mediaLibrary = mediaLibrary;
[mediaLibrary addObserver:self
forKeyPath:@"mediaSources"
options:0
context:(__bridge void *)@"mediaLibraryLoaded"];
[mediaLibrary mediaSources]; // returns nil and starts asynchronous loading
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
{
if (context == (__bridge void *)@"mediaLibraryLoaded") {
// Media Library is loaded now, we can access mediaSources
MLMediaSource *mediaSource = [self.mediaLibrary.mediaSources objectForKey:@"com.apple.Photos"];
}
}
库背后的概念是您必须请求它读取对象的属性,这会返回一个空引用。然后你用一个键值观察器订阅这个属性,你等到它被加载。然后就可以用同样的原理找回下一个 child ……
关于objective-c - 以编程方式访问 Mac OS X 上的照片库 : PhotoKit/Photos Framework for Mac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30144547/