Quantcast
Channel: Programmatic access to the Photos Library on Mac OS X: PhotoKit / Photos Framework for Mac - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by vtukhtarov for Programmatic access to the Photos Library on Mac OS X: PhotoKit / Photos Framework for Mac

$
0
0

Based on Pierre F answer I extend code for displaying url's for all photos:

- (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);        }    }}

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>