iOS - UIImagePickerController to display images from a specific Album only - ios

I'm currently using ALAssetsLibrary to save my images into a custom album using writeImageToSavedPhotosAlbum.
[self.assetsLibrary addAssetsGroupAlbumWithName:album
resultBlock:^(ALAssetsGroup *group) {
NSLog(#"added album:%#", album);
}
failureBlock:^(NSError *error) {
NSLog(#"error adding album");
}];
__block ALAssetsGroup* groupToAddTo;
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:album]) {
NSLog(#"found album %#", album);
groupToAddTo = group;
}
}
failureBlock:^(NSError* error) {
NSLog(#"failed to enumerate albums:\nError: %#", [error localizedDescription]);
}];
CGImageRef img = [imageToSave CGImage];
[self.assetsLibrary writeImageToSavedPhotosAlbum:img
metadata:metadata
completionBlock:^(NSURL* assetURL, NSError* error) {
if (error.code == 0) {
NSLog(#"saved image completed:\nurl: %#", assetURL);
// try to get the asset
[self.assetsLibrary assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
// assign the photo to the album
[groupToAddTo addAsset:asset];
NSLog(#"Added %# to %#", [[asset defaultRepresentation] filename], album);
}
failureBlock:^(NSError* error) {
NSLog(#"failed to retrieve image asset:\nError: %# ", [error localizedDescription]);
}];
}
else {
NSLog(#"saved image failed.\nerror code %li\n%#", (long)error.code, [error localizedDescription]);
}
}];
I would like to have my UIImagePickerController to select images from the custom album ONLY, or a way to select only images saved by my app. How can I do it?

Related

ALAssetsLibrary addAssetsGroupAlbumWithName is not working on iOS 9

I need to add group with name "MyGroupName" in ALAssetsLibrary . So I have used below code.
ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];
__weak ALAssetsLibrary *lib = library;
[library addAssetsGroupAlbumWithName:#"MyGroupName" resultBlock:^(ALAssetsGroup *group) {
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *g, BOOL *stop)
{
if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:#"MyGroupName"]) {
NSLog(#"group created with name 'MyGroupName'");
}
}failureBlock:^(NSError *error){
NSLog(#"failure %#",error);
}
];
} failureBlock:^(NSError *error) {
NSLog(#"failure %#",error);
}];
but inside "enumerateGroupsWithTypes" , group "g" is always nil in iOS 9.3.1 (iphone 6). its working correctly and group created with name "MyGroupName" on iOS 9.3.1 iphone 5. I want to know why above code is not working on iphone 6 and is there any solution to make it work ?
Please help me. Thanks in advance
1) First Import
#import <Photos/Photos.h>
#import <Photos/PHAsset.h>
#import <AssetsLibrary/AssetsLibrary.h>
2) Set property for ALAsset
#property (nonatomic, strong) ALAssetsLibrary* assetsLibrary;
3) Then allocate ALAsset library in your .m file
- (ALAssetsLibrary*)assetsLibrary
{
if (!_assetsLibrary) {
_assetsLibrary = [[ALAssetsLibrary alloc] init];
[ALAssetsLibrary disableSharedPhotoStreamsSupport];
}
return _assetsLibrary;
}
4 ) Now create method for save image to custom album
- (void)saveImageDatas:(UIImage*)imageDatas toAlbum:(NSString*)album withCompletionBlock:(void(^)(NSError *error))block
{
` if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
NSMutableArray* assets = [[NSMutableArray alloc]init];
PHAssetChangeRequest* assetRequest;
#autoreleasepool {
assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:imageDatas];
[assets addObject:assetRequest.placeholderForCreatedAsset];
}
__block PHAssetCollectionChangeRequest* assetCollectionRequest = nil;
PHFetchResult* result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[result enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
PHAssetCollection* collection = (PHAssetCollection*)obj;
if ([collection isKindOfClass:[PHAssetCollection class]]) {
if ([[collection localizedTitle] isEqualToString:album]) {
assetCollectionRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
[assetCollectionRequest addAssets:assets];
*stop = YES;
}
}
}];
if (assetCollectionRequest == nil) {
assetCollectionRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:album];
[assetCollectionRequest addAssets:assets];
}
}
completionHandler:^(BOOL success, NSError *error) {
if (block) {
block(error);
}
}];
}
else {
__weak ALAssetsLibrary* lib = [self assetsLibrary];
[[self assetsLibrary] writeImageDataToSavedPhotosAlbum:UIImageJPEGRepresentation(imageDatas, 1.0) metadata:nil completionBlock:^(NSURL* assetURL, NSError* error) {
if (error != nil) {
return;
}
__block BOOL albumWasFound = NO;
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup* group, BOOL* stop) {
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:album]) {
albumWasFound = YES;
[lib assetForURL:assetURL resultBlock:^(ALAsset* asset){
[group addAsset:asset];
if (block) {
block(nil);
}
}failureBlock:^(NSError* error) {
if (block) {
block(error);
}
}];
return;
}
if (group == nil && albumWasFound == NO) {
[lib addAssetsGroupAlbumWithName:album resultBlock:^(ALAssetsGroup* group) {
} failureBlock:^(NSError* error) {
[lib assetForURL:assetURL resultBlock:^(ALAsset* asset){
[group addAsset:asset];
if (block) {
block(nil);
}
}failureBlock:^(NSError* error) {
if (block) {
block(error);
}
}];
}];
}
} failureBlock:^(NSError* error) {
if (block) {
block(error);
}
}];
}];
}
}
5 ) Now call this method to save the image like
[self saveImageDatas:myimage toAlbum:#"MyGroupName" withCompletionBlock:^(NSError *error) {
if (!error) {
NSLog(#"Sucess");
}
}];
"myimage" is your image that you want to save.
Please try this on:
ALAssetsLibrary* libraryFolder = [[ALAssetsLibrary alloc] init];
[libraryFolder addAssetsGroupAlbumWithName:#"My Album" resultBlock:^(ALAssetsGroup *group)
{
NSLog(#"Adding Folder:'My Album', success: %s", group.editable ? "Success" : "Already created: Not Success");
} failureBlock:^(NSError *error)
{
NSLog(#"Error: Adding on Folder");
}];
Or Please check this link also
http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/

Save photos to album iOS, do not save all of them

I am trying to save photos from a app ,create the album and save the photos to it, but it don't works properly.
Sometime it save one or another photo, but never all of them.
Best regards to everyone and thanks!
for(PFObject *pf in imageFilesArray)
{
PFObject *imageObject = pf;
PFFile *imageFile = [imageObject objectForKey:#"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];
[self.library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationUp completionBlock:^(NSURL *assetURL, NSError *error) {
if(assetURL) {
[self.library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[self addAsset:asset toGroup:#"App Album" inLib:self.library];
NSLog(#"Adicionado");
} failureBlock:^(NSError *error) {
NSLog(#"%#",error);
}];
}
}];
}
}];
}
[hud hide:NO];
The - (void) addAsset:(ALAsset*)a toGroup:(NSString*)name inLib:(ALAssetsLibrary*)lib method
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:name]) {
[group addAsset:a];
*stop = YES;
NSLog(#"added asset to EXISTING group");
}
if(!group) {
[lib addAssetsGroupAlbumWithName:name resultBlock:^(ALAssetsGroup *group) {
[group addAsset:a];
NSLog(#"added asset to NEW group");
} failureBlock:^(NSError *error) {
NSLog(#"e: %#", error);
}];
}
} failureBlock:^(NSError *error) {
NSLog(#"e: %#", error);
}];
I've discussed this issue with someone HERE.
... when there's no photo album available, only part images are saved in a for loop. I tested the code and found that -addAssetsGroupAlbumWithName:resultBlock:failureBlock: will process asynchrony, this method will be dispatched several times when the album is not created (this might take some time).
In this case, you can just create a photo album before image adding process.
[self.assetsLibrary addAssetsGroupAlbumWithName:<your_custom_album_name>
resultBlock:nil
failureBlock:nil];
And feel free to take a look at this lib: ALAssetsLibrary-CustomPhotoAlbum, it might do lots helpful things for u ;)

saving image to custom photo album in iOS7

I'm trying to save a photo taken with the camera to a custom photo album in iOS7.
my code looks like this:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block ALAssetsGroup* folder;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:#"UrbanAlphabets"])
{
folder = group;
}
}
failureBlock:^(NSError* error)
{
// Error handling.
}];
[library writeImageToSavedPhotosAlbum:(__bridge CGImageRef)(image)
metadata:nil
completionBlock:^(NSURL* assetURL, NSError* error)
{
if (error.code == 0)
{
// Get the asset
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset)
{
// Assign the photo to the album
[folder addAsset:asset];
NSLog(#"success");
}
failureBlock:^(NSError* error)
{
// Error handling.
NSLog(#"error1");
}];
}
else
{
// Error handling.
NSLog(#"error");
}
}];
and actually the console logs "success", so I guess everything should be fine, but it doesn't put the photo into the folder... I pretty much copypasted the code from here
http://www.ggkf.com/iphone/save-a-photo-to-a-folder-in-photo-library
any ideas?
this made it work
//write to photo library
NSString *albumName=#"Urban Alphabets";
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block ALAssetsGroup* groupToAddTo;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:albumName]) {
groupToAddTo = group;
}
}
failureBlock:^(NSError* error) {
}];
CGImageRef img = [croppedImage CGImage];
[library writeImageToSavedPhotosAlbum:img
metadata:nil
completionBlock:^(NSURL* assetURL, NSError* error) {
if (error.code == 0) {
// try to get the asset
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
// assign the photo to the album
[groupToAddTo addAsset:asset];
}
failureBlock:^(NSError* error) {
}];
}
else {
}
}];

Creating image folders in IOS

All,
When the user of my app takes a photo, it will be saved but i want it to save in a new folder called 'Jasons Photos' and not the usual Camera Roll album.
I know of the completion block called save image:image toAlbum 'Jasons Album' with completion block, but I get an error complaining that 'No visible #interface for AlAssetslibrary declares the selector 'save image to album completion block' and its driving me crazy.
I have created an #property for the ALAssets library and i have included <AssetsLibrary> in my project.
thanks
You first need to write it to the savedPhotos, then get the ALAsset and add it to a new group
#import "QBViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#implementation QBViewController
- (void)viewDidLoad
{
[super viewDidLoad];
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib writeImageToSavedPhotosAlbum:[[UIImage imageNamed:#"polter"] CGImage] orientation:ALAssetOrientationUp completionBlock:^(NSURL *assetURL, NSError *error) {
if(assetURL) {
[lib assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[self addAsset:asset toGroup:#"forest" inLib:lib];
} failureBlock:^(NSError *error) {
NSLog(#"e: %#", error);
}];
}
else {
NSLog(#"e: %#", error);
}
}];
}
- (void) addAsset:(ALAsset*)a toGroup:(NSString*)name inLib:(ALAssetsLibrary*)lib {
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:name]) {
[group addAsset:a];
*stop = YES;
NSLog(#"added asset to EXISTING group");
}
if(!group) {
[lib addAssetsGroupAlbumWithName:name resultBlock:^(ALAssetsGroup *group) {
[group addAsset:a];
NSLog(#"added asset to NEW group");
} failureBlock:^(NSError *error) {
NSLog(#"e: %#", error);
}];
}
} failureBlock:^(NSError *error) {
NSLog(#"e: %#", error);
}];
}
#end

Save a photo to a folder in photo library

So I know that using the following saves an image to the general camera roll:
UIImage *image = imageView.image;
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
But what I want to do is save the photos to a specifically named folder in my photo library. For instance, any photo I take with Instagram is saved to an Instagram folder in my library. This also works for snapchat.
First you need to create a folder:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:folderName
resultBlock:^(ALAssetsGroup *group)
{
NSLog(#"Added folder:%#", folderName);
}
failureBlock:^(NSError *error)
{
NSLog(#"Error adding folder");
}];
Then, find the folder:
__block ALAssetsGroup* folder;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:folderName])
{
folder = group;
}
}
failureBlock:^(NSError* error)
{
// Error handling.
}];
And add your photo to it.
Save the Image to Asset Library, and put it into the album:
[library writeImageToSavedPhotosAlbum:yourImage
metadata:[info objectForKey:UIImagePickerControllerMediaMetadata]
completionBlock:^(NSURL* assetURL, NSError* error)
{
if (error.code == 0)
{
// Get the asset
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset)
{
// Assign the photo to the album
[folder addAsset:asset];
}
failureBlock:^(NSError* error)
{
// Error handling.
}];
}
else
{
// Error handling.
}
}];

Resources