2
votes

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);
}];
1

1 Answers

1
votes

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 ;)