Skip to content

Instantly share code, notes, and snippets.

@nicwise nicwise/gist:1388126
Created Nov 23, 2011

Embed
What would you like to do?
failing ALAssetLibrary
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.AssetsLibrary;
namespace Owen
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
// class-level declarations
UIWindow window;
OwenViewController viewController;
ALAssetsLibrary lib;
UIImage image;
NSDictionary dict = new NSDictionary ();
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
viewController = new OwenViewController ();
window.RootViewController = viewController;
window.MakeKeyAndVisible ();
lib = new ALAssetsLibrary ();
//this is embedded into my app - just set to "content"
image = UIImage.FromFile ("armbears.jpg");
//this works! yay! passing a NSData JPEG
lib.WriteImageToSavedPhotosAlbum (image.AsJPEG (), dict, delegate(NSUrl assetUrl, NSError error) {
if (error != null)
{
Console.WriteLine (error.LocalizedDescription);
}
Console.WriteLine ("WRITE: >>>" + assetUrl);
});
//this does NOT work. passing a UIImage. Apple docs say a CGImage, so.... is MT doing something
// tricky in the background? Or is the binding incorrect??
lib.WriteImageToSavedPhotosAlbum (image, dict, delegate(NSUrl assetUrl, NSError error) {
if (error != null)
{
Console.WriteLine ("error 2" + error.LocalizedDescription);
}
Console.WriteLine ("WRITE2: >>>" + assetUrl);
});
//this is all async, so you are seldom going to see the image you put in, come out right away.
lib.Enumerate (ALAssetsGroupType.All, (ALAssetsGroup @group, ref bool stop) => {
if (group != null)
{
stop = false;
group.SetAssetsFilter (ALAssetsFilter.AllAssets);
group.Enumerate ((ALAsset asset, int index, ref bool stop2) =>
{
if (asset != null)
{
stop2 = false;
Console.WriteLine ("ENUMERATE Asset: " + asset.DefaultRepresentation.Url);
}
}
);
}
}, (error) => {
Console.WriteLine ("Error?");
if (error != null)
{
Console.WriteLine ("Error!" + error.LocalizedDescription);
}
});
//you wanna change this... look in the debug log for URLs
string testUrl = "assets-library://asset/asset.JPG?id=069817D9-2412-4C26-923B-BA6DC745D541&ext=JPG";
lib.AssetForUrl (new NSUrl (testUrl), (asset) => {
Console.WriteLine ("Read asset back");
if (asset != null)
{
Console.WriteLine ("{0} - {1}", asset.DefaultRepresentation.Filename, asset.AssetType);
}
}, (error) => {
Console.WriteLine ("error?");
if (error != null)
{
Console.WriteLine ("Error: " + error.LocalizedDescription);
}
});
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.