在Mac OS X上以编程方式访问照片库:Mac的PhotoKit/Photos Framework [英] Programmatic access to the Photos Library on Mac OS X: PhotoKit / Photos Framework for Mac

查看:89
本文介绍了在Mac OS X上以编程方式访问照片库:Mac的PhotoKit/Photos Framework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,有一个 Photos Framework aka PhotoKit 支持iOS开发人员可以访问iPhone和iPad上的照片库,并检索图片/视频及其元数据.

In Objective-C, there's a Photos Framework a.k.a. PhotoKit which enables iOS developers to access the photos library on iPhone and iPad and to retrieve the pictures/videos along with their metadata.

Mac开发人员将如何执行类似的任务? 看来PhotoKit仅在iOS 8.0中可用.是否有与Mac OS X相同的Photos Framework?

How would Mac developers perform a similar task? It seems PhotoKit is only available in iOS 8.0. Is there an equivalent of the Photos Framework for Mac OS X?

推荐答案

用法:

@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"];
   }
}

该库背后的概念是,您必须请求它读取对象的属性,该属性返回空引用.然后,您使用键值观察器订阅此属性,然后等待它加载.然后,您可以使用相同的原理检索下一个孩子,依此类推...

The concept behind the library is that you have to request it to read an attribute of an object, which returns an empty reference. Then you subscribe to this attribute with a key-value-observer and you wait till it is loaded. Then you can retrieve the next child with the same principle and so on...

这篇关于在Mac OS X上以编程方式访问照片库:Mac的PhotoKit/Photos Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆