Skip to content

Instantly share code, notes, and snippets.

@nicwise
Created February 6, 2013 14:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicwise/4722949 to your computer and use it in GitHub Desktop.
Save nicwise/4722949 to your computer and use it in GitHub Desktop.
using System;
using MonoTouch.AssetsLibrary;
using MonoTouch.Foundation;
namespace BigTed
{
public class ImageLibraryUtils
{
private ALAssetsLibrary assetsLibrary;
public ImageLibraryUtils()
{
assetsLibrary = new ALAssetsLibrary();
}
public void SaveImageToLibrary(NSData image, string albumName, Action<bool, string> complete)
{
assetsLibrary.WriteImageToSavedPhotosAlbum(image, new NSDictionary(), delegate(NSUrl assetURL, NSError error) {
if (error != null)
{
complete(false, error.Description);
return;
}
AddAssetUrlToAlbum(assetURL, albumName, complete);
});
}
public void AddAssetUrlToAlbum(NSUrl assetUrl, string albumName, Action<bool, string> complete)
{
bool wasFound = false;
assetsLibrary.Enumerate(ALAssetsGroupType.Album, delegate(ALAssetsGroup @group, ref bool stop) {
try
{
if (group != null && albumName == @group.Name)
{
wasFound = true;
assetsLibrary.AssetForUrl(assetUrl, delegate(ALAsset asset) {
@group.AddAsset(asset);
complete(true, "Image was added to the album");
}, delegate(NSError error) {
complete(false, error.Description);
});
return;
}
if (!wasFound && @group == null)
{
assetsLibrary.AddAssetsGroupAlbum(albumName, delegate(ALAssetsGroup @group2) {
assetsLibrary.AssetForUrl(assetUrl, delegate(ALAsset asset) {
@group2.AddAsset(asset);
complete(true, "Image was added to the album");
}, delegate(NSError error) {
complete(false, error.Description);
});
}, delegate(NSError error) {
complete(false, error.Description);
});
}
} catch (Exception ex)
{
complete(false, "Error saving to the library");
}
}, delegate(NSError error) {
complete(false, error.Description);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment