Question

I want to access and copy all the photos from one album in my iPhone photo album to my app's document folder AUTOMATICALLY WHEN THE APP STARTS.

There are a lot of questions/answers on here on using the UIImagePicker. I know I can do it with the image picker and have the user select the photos but I would really like it if I could do it without the user selecting the photos.

I have used the following question/answer to help with the code below: How to retrieve the most recent photo from Camera Roll on iOS?

I was able to successfully save the one image in my documents folder but cannot figure out how to save more than one at a time.

     ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes: ALAssetsGroupAlbum  //ALAssetsGroupAlbum before I CHANGED it
                             usingBlock:^(ALAssetsGroup *group, BOOL *stop)  { //
                                 if (nil != group) {
                                     // be sure to filter the group so you only get photos
                                     [group setAssetsFilter:[ALAssetsFilter allPhotos]];

                                     [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex: group.numberOfAssets - 1 ]// -1
                                                            options:0
                                                         usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {


                                                        if (nil != result)
                                                              {
                                                                ALAssetRepresentation *repr = [result defaultRepresentation];
                                                                  // this is the most recent saved photo
                                                                  UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];
                                                                  // we only need the first (most recent) photo -- stop the enumeration
                                                                  // *stop = YES;

                                                                  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
                                                                  NSString *documentsDirectory = [paths objectAtIndex:0];

                                                                  int num = 1;
                                                                  NSString* path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"%@-%d.png",@"boxOneImage", num]];
                                                                  num++;

                                                                  NSData* data = UIImagePNGRepresentation(img);
                                                                  [data writeToFile:path atomically: YES];



                                                              }
                                                          }];
                                 }

                                 *stop = NO;
                             } failureBlock:^(NSError *error) {
                                 NSLog(@"error: %@", error);
                             }]; 

UPDATE: I was able to figure it out and load images from just an album not the camera roll to my app documents folder using the following code:

    __block int num = 1;

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
    if (nil != group)
    {
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];

        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){

            if (nil != result)
            {
                ALAssetRepresentation *repr = [result defaultRepresentation];
                // this is the most recent saved photo
                UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];

                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];


                NSString* path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"%@-%d.png",@"boxOneImage", num]];
                num++;

                NSData* data = UIImagePNGRepresentation(img);
                [data writeToFile:path atomically: YES];

            }
        }];
    }

    *stop = NO;
} failureBlock:^(NSError *error)
{
    NSLog(@"error: %@", error);
}];

If anyone has or knows a better way of doing this, then please share. I would love to see it and learn from it. Thanks!!!

Was it helpful?

Solution

I think your existing method itself will work with a small change.

Remove int num = 1; which is in the block and add __block int num = 1; outside the block, may be before assetsLibrary initialisation.

Or a much better solution, remove variable num completely, make use of index+1 passed to the block!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top