2015-05-09 79 views
6

在Objective-C中,有一個Photos Framework a.k.a. PhotoKit,它使iOS開發人員可以訪問iPhone和iPad上的照片庫並檢索照片/視頻其元數據。在Mac OS X上編程訪問照片庫:PhotoKit/Photos Framework for Mac

Mac開發人員將如何執行類似的任務? 看來PhotoKit僅適用於iOS 8.0。是否有相當於Mac OS X的照片框架?

+0

https://developer.apple。 com/library/mac/documentation/MediaLibrary/Reference/MediaLibraryFrameworkReference/index.html#// apple_ref/doc/uid/TP40012956 – matt

+0

Thanks matt!這就是訣竅! –

+0

上面提到的PhotoKit網頁說它也可用於「macOS 10.11+」。 – Gabriel

回答

10

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

庫背後的概念是,你必須要求它來讀取一個對象,它返回一個空引用的屬性。然後,您使用鍵值觀察者訂閱此屬性,然後等待其加載。然後,你可以用相同的原理檢索下一個孩子等等...

+0

我很難從此代碼示例中訪問各個照片。你可以詳細說明如何使用MLMediaSource?該文檔[非常糟糕](http://stackoverflow.com/q/34835821/1418688)... – Nerrolken

+0

我同意,文檔不清楚,你必須嘗試與調試器的事情,以瞭解如何下載到MLMediaObjects ... –

+0

如何從路徑訪問照片庫圖像?示例照片庫位於/Users/admin/Desktop/xyz.photoslibrary中。 –

4

基於皮埃爾˚F答案我顯示的URL的所有照片延長代碼:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 

    NSDictionary *options = @{ 
           MLMediaLoadSourceTypesKey: @(MLMediaSourceTypeImage), 
           MLMediaLoadIncludeSourcesKey: @[MLMediaSourcePhotosIdentifier] 
           }; 

    self.mediaLibrary = [[MLMediaLibrary alloc] initWithOptions:options]; 

    [self.mediaLibrary addObserver:self 
         forKeyPath:@"mediaSources" 
          options:0 
          context:(__bridge void *)@"mediaLibraryLoaded"]; 

    [self.mediaLibrary.mediaSources objectForKey:MLMediaSourcePhotosIdentifier]; 
} 


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
         change:(NSDictionary *)change context:(void *)context 
{ 
    MLMediaSource *mediaSource = [self.mediaLibrary.mediaSources objectForKey:MLMediaSourcePhotosIdentifier]; 

    if (context == (__bridge void *)@"mediaLibraryLoaded") 
    { 
     [mediaSource addObserver:self 
          forKeyPath:@"rootMediaGroup" 
           options:0 
           context:(__bridge void *)@"rootMediaGroupLoaded"]; 

     [mediaSource rootMediaGroup]; 
    } 
    else if (context == (__bridge void *)@"rootMediaGroupLoaded") 
    { 
     MLMediaGroup *albums = [mediaSource mediaGroupForIdentifier:@"TopLevelAlbums"]; 

     for (MLMediaGroup *album in albums.childGroups) 
     { 
      NSString *albumIdentifier = [album.attributes objectForKey:@"identifier"]; 

      if ([albumIdentifier isEqualTo:@"allPhotosAlbum"]) 
      { 
       self.allPhotosAlbum = album; 

       [album addObserver:self 
         forKeyPath:@"mediaObjects" 
          options:0 
          context:@"mediaObjects"]; 

       [album mediaObjects]; 

       break; 
      } 
     } 
    } 
    else if (context == (__bridge void *)@"mediaObjects") 
    { 
     NSArray * mediaObjects = self.allPhotosAlbum.mediaObjects; 

     for(MLMediaObject * mediaObject in mediaObjects) 
     { 
      NSURL * url = mediaObject.URL; 

      NSLog(url.path); 
     } 
    } 
} 
+0

這看起來棒極了,我一回家就會試試!我很好奇,你有關於如何從MLMediaObject獲取GPS位置的任何想法?我有[關於這個問題](http://stackoverflow.com/q/34932712/1418688)現在打開,一般來說,關於媒體庫框架的信息很少。 – Nerrolken

+0

順便說一句,我試圖接受你對我的其他問題的答案,但它似乎已被刪除?如果你用這段代碼重新發布它,我會很高興今晚向你獎勵賞金! :) – Nerrolken

+0

我試圖按照您的示例,但是@keypath是什麼? Xcode(7.2.1)抱怨,谷歌一直沒有幫助:( –