Skip to content

Instantly share code, notes, and snippets.

@beeradmoore
Created April 21, 2014 13:04
Show Gist options
  • Save beeradmoore/11142147 to your computer and use it in GitHub Desktop.
Save beeradmoore/11142147 to your computer and use it in GitHub Desktop.
Sample of UIPickerController and ALAssetsLibrary
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.AssetsLibrary;
namespace ImagePickerSample
{
[Register("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;
UIImagePickerController imagePickerController;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
window = new UIWindow(UIScreen.MainScreen.Bounds);
// Create image picker.
imagePickerController = new UIImagePickerController();
// Set the source.
imagePickerController.SourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
// Handle what happens when we click the image.
imagePickerController.FinishedPickingMedia += delegate(object sender, UIImagePickerMediaPickedEventArgs e)
{
// Normally we would dismiss this viewcontroller, but not in this sample.
// Get the reference URL.
NSString key = new NSString("UIImagePickerControllerReferenceURL");
if (e.Info.ContainsKey(key))
{
// Display it.
Console.WriteLine(e.Info[key].ToString());
UIAlertView alert = new UIAlertView("", e.Info[key].ToString(), null, "Ok", null);
alert.Show();
}
else
{
// In theory this will never ever happen.
UIAlertView alert = new UIAlertView("Error", "No refernece found.", null, "Ok", null);
alert.Show();
}
};
window.RootViewController = imagePickerController;
window.MakeKeyAndVisible();
/*
// Put your asset string here, will output in the console output when you click an image, copy/paste it here.
string assetString = "assets-library://asset/asset.JPG?id=477B5C5D-8A30-49E1-9F4A-CA89F5751CB4&ext=JPG";
ALAssetsLibrary asetLibrary = new ALAssetsLibrary();
asetLibrary.AssetForUrl(new NSUrl(assetString), delegate(ALAsset asset)
{
Console.WriteLine("AssetType: " + asset.AssetType);
// Loads a UIImage from the refernece URL we got originally.
UIImage image = UIImage.FromImage(asset.DefaultRepresentation.GetImage());
Console.WriteLine("ImageSize: " + image.Size);
}, delegate(NSError error)
{
Console.WriteLine("Load Asset Error: " + error.LocalizedDescription);
});
*/
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment