Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

objective c - Iterate through photo library on OS X?

I'm trying to iterate through a user's photo library on OS X. On iOS, I would use the Photos framework, but apparently that's not available on OS X, and we're supposed to use the Media Library framework instead. However, while I was able to use the code linked above to get access to a MLMediaSource object, I'm having a hard time figuring out how to iterate through the photo assets.

The Media Library Framework documentation makes reference to a mediaObjectForIdentifier: method, which sounds promising, but doesn't indicate what sort of identifiers should be used. I'm fairly experienced with iOS, but a complete n00b on OS X, so I'm feeling a little lost.

If I just wanted to iterate through a user's library, NSLog'ing each photo, how might I go about that? Either using the Media Library Framework, or another framework so long as it works for the current Photos library?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This framework isn't hard to work with, but it is tedious because it async/lazy loads the properties, and KVO is the only want to be notified about the async completion.

If you want to iterate the photos, you don't need to know the identifiers in advance.

Create a media library:

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

Add a KVO observer for mediaSources. Access mediaSources, if non-nil, go to the next step, otherwise go to the next step when your KVO observer fires.

In the next step, iterate the sources, add a KVO observer on rootMediaGroup, and access rootMediaGroup on each source. If it is non-nil, call your iterator now, otherwise call it from the KVO notification handler.

For each rootMediaGroup, follow the same strategy as above, but for mediaObjects. The media objects are the things you are ultimately after.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...