Save video to custom album in camera roll

I can save the image to a custom album in the iPhone camera roll. For example, the title of the album might be "Fun" and all the images I take will go into that folder. I came across a helper class here that works really well to accomplish what I want.

However, the method I'm using doesn't save the video to a custom folder, but the folder is created - it's just empty from any videos I try to save there. I tried to debug it without any success. This method is located here:

-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
__block BOOL albumWasFound = NO;

    //search all photo albums in the library
[self enumerateGroupsWithTypes:ALAssetsGroupAlbum 
                    usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                        //compare the names of the albums
                        if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
                            //target album is found
                            albumWasFound = YES;

                            NSData *data = [NSData dataWithContentsOfURL:assetURL];
                            if ([data length] > 0) {
                                //get a hold of the photo asset instance
                                [self assetForURL: assetURL 
                                      resultBlock:^(ALAsset *asset) {

                                          [group addAsset: asset];

                                          //run the completion block
                                          completionBlock(nil);

                                      } failureBlock: completionBlock];
                            }

                            //album was found, bail out of the method
                            return;
                        }

                        if (group==nil && albumWasFound==NO) {
                            //photo albums are over, target album does not exist, thus create it

                            __weak ALAssetsLibrary* weakSelf = self;

                            //create new assets album
                            [self addAssetsGroupAlbumWithName:albumName 
                                                  resultBlock:^(ALAssetsGroup *group) {

                                                      //get the photo instance
                                                      [weakSelf assetForURL: assetURL 
                                                                    resultBlock:^(ALAsset *asset) {
                                                                        NSLog(@"asset: %@",asset);
                                                                        //add photo to the newly created album
                                                                        [group addAsset: asset];

                                                                        //call the completion block
                                                                        completionBlock(nil);

                                                                    } failureBlock: completionBlock];

                                                  } failureBlock: completionBlock];

                            //should be the last iteration anyway, but just in case
                            return;
                        }

                    } failureBlock: completionBlock];

}

      

Does anyone know how to properly save a video to a custom album in the photo library?

0


source to share


1 answer


Try it, it works for me.

-(void)saveVideo:(NSURL *)videoUrl toAlbum:(NSString*)albumName withCompletionBlock:  (SaveImageCompletion)completionBlock
{
    //write the image data to the assets library (camera roll)
    [self writeVideoAtPathToSavedPhotosAlbum:videoUrl completionBlock:^(NSURL* assetURL, NSError* error) {

                       //error handling
                       if (error!=nil) {
                           completionBlock(error);
                           return;
                       }

                       //add the asset to the custom photo album
                       [self addAssetURL: assetURL
                                 toAlbum:albumName
                     withCompletionBlock:completionBlock];

                   }];

      



}

0


source







All Articles