Created
January 30, 2017 18:33
-
-
Save chamons/9f9b6aa1f4b45ba8603cd5ba09a79b1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using AppKit; | |
using Foundation; | |
using MediaLibrary; | |
namespace HttpTest | |
{ | |
public partial class ViewController : NSViewController | |
{ | |
public ViewController (IntPtr handle) : base (handle) | |
{ | |
} | |
MLMediaLibrary mediaLibrary; | |
NSTextField textView; | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
textView = new NSTextField (View.Frame) { BackgroundColor = NSColor.Yellow, StringValue = "More info coming..." }; | |
View.AddSubview (textView); | |
mediaLibrary = new MLMediaLibrary (NSDictionary<NSString, NSObject>.FromObjectsAndKeys | |
( | |
new NSObject[] { NSNumber.FromUInt64 ((ulong)MLMediaSourceType.Image) }, | |
new NSString[] { MLMediaLibrary.MediaLoadSourceTypesKey } | |
)); | |
var mediaSources = mediaLibrary.MediaSources; | |
if (mediaSources != null) | |
HandleMediaSource (mediaSources); | |
else | |
mediaLibrary.AddObserver ("mediaSources", 0, c => HandleMediaSource (mediaLibrary.MediaSources)); | |
} | |
void HandleMediaSource (NSDictionary<NSString, MLMediaSource> mediaSources) | |
{ | |
MLMediaSource iTunesSource; | |
var success = mediaLibrary.MediaSources.TryGetValue (MLMediaSource.MediaSourcePhotosIdentifier, out iTunesSource); | |
if (success) { | |
MLMediaGroup grp = iTunesSource.RootMediaGroup; | |
if (grp != null) | |
HandleGroup (grp); | |
else | |
iTunesSource.AddObserver ("rootMediaGroup", 0, c => HandleGroup (iTunesSource.RootMediaGroup)); | |
} | |
} | |
void HandleGroup (MLMediaGroup group) | |
{ | |
MLMediaObject[] objects = group.MediaObjects; | |
if (objects != null) | |
HandleMediaObject (objects); | |
else | |
group.AddObserver ("mediaObjects", 0, c => HandleMediaObject (group.MediaObjects)); | |
} | |
void HandleMediaObject (MLMediaObject[] objects) | |
{ | |
textView.StringValue = string.Format ("Found {0} items", objects.Length); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment