Created
February 6, 2013 14:42
-
-
Save nicwise/4722949 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 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