在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的照片框架?
在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的照片框架?
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"];
}
}
庫背後的概念是,你必須要求它來讀取一個對象,它返回一個空引用的屬性。然後,您使用鍵值觀察者訂閱此屬性,然後等待其加載。然後,你可以用相同的原理檢索下一個孩子等等...
我很難從此代碼示例中訪問各個照片。你可以詳細說明如何使用MLMediaSource?該文檔[非常糟糕](http://stackoverflow.com/q/34835821/1418688)... – Nerrolken
我同意,文檔不清楚,你必須嘗試與調試器的事情,以瞭解如何下載到MLMediaObjects ... –
如何從路徑訪問照片庫圖像?示例照片庫位於/Users/admin/Desktop/xyz.photoslibrary中。 –
基於皮埃爾˚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);
}
}
}
https://developer.apple。 com/library/mac/documentation/MediaLibrary/Reference/MediaLibraryFrameworkReference/index.html#// apple_ref/doc/uid/TP40012956 – matt
Thanks matt!這就是訣竅! –
上面提到的PhotoKit網頁說它也可用於「macOS 10.11+」。 – Gabriel