Iterating through a photo library on OS X?

I am trying to iterate through a user photo library in OS X. On iOS, I would use the Photos framework, but apparently that is not available in OS X , and we should use the Media Library environment. However, although I managed to use the code linked above to access the MLMediaSource object, it is difficult for me to determine how to iterate using photo archives.

The media library documentation refers to the mediaObjectForIdentifier: method, which sounds promising, but does not indicate which identifiers should be used. I'm pretty experienced with iOS, but full n00b on OS X, so I feel a little lost.

If I just wanted to iterate through the user library, NSLog'ing each photo, how can I do this? Either use the media library environment, or another structure if it works for the current photo library?

+3
source share
1 answer

This card is not difficult to work with, but it is tedious because it loads properties asynchronously / lazy, and KVO is the only one who wants to receive async completion notification.

If you want to iterate photos, you do not need to know identifiers in advance.

Create a media library:

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

Add KVO Observer for mediaSources . Get access to media resources, if not zero, go to the next step, otherwise go to the next step when your KVO observer is triggered.

In the next step, iterating over the sources, adding a KVO observer to rootMediaGroup and rootMediaGroup each source. If it is not equal to zero, call your iterator now, otherwise call it from the KVO notification handler.

For each rootMediaGroup, follow the same strategy as above, but for mediaObjects . Media objects are what you end up after.

+3
source

Source: https://habr.com/ru/post/986885/


All Articles