1

I have download the .mp4 file from sever to document directory and called the writeVideoAtPathToSavedPhotosAlbum method to save to gallery but its nor working.its throwing asset url nil.Can anyone plz help me with this issue

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.mp4"];
    NSURL *movieURL = [NSURL fileURLWithPath:path];
    ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
    NSLog(@"Moview URL:%@",movieURL);
    NSURL *url = [movieURL copy];

    [library writeVideoAtPathToSavedPhotosAlbum:url
                                completionBlock:^(NSURL *assetURL, NSError *error)
    {
        NSLog(@"Asset Url:%@",assetURL);
        if(!error) {
            NSLog(@"\t ! Error");
            NSLog(@"\t Error: %@", [error localizedDescription]);
            NSLog(@"\t Error code %d", [error code]);
        }

        if(error != nil) {
            NSLog(@"\t ERROR != NIL");
            NSLog(@"\t Error - Image Failed To Save With Error: %@", [error localizedDescription]);
            NSLog(@"\t Error code %d", [error code]);
        }

        if(error == nil) {
            NSLog(@"\t ERROR == NIL");
        }
    }];
Vvk
  • 4,031
  • 29
  • 51
leni
  • 33
  • 1
  • 6
  • What is the log of this line:- NSLog(@"Moview URL:%@",movieURL);? comment it here – Vizllx Feb 09 '16 at 06:30
  • What is the use of this line:- NSURL *url = [movieURL copy]; – Vizllx Feb 09 '16 at 06:32
  • url is asset url of video location in document directory – leni Feb 09 '16 at 06:41
  • are you getting any value in variable "URL", then log it? – Vizllx Feb 09 '16 at 06:44
  • URL value is there file:///var/mobile/Containers/Data/Application/E0B3785E-D991-4FE3-A67A-C49A3D2DF032/Documents/test.mp4 but inside writeVideoAtPathToSavedPhotosAlbum ,the asset url is nil.Its not saving to gallery as well – leni Feb 09 '16 at 06:49
  • http://stackoverflow.com/questions/4493673/save-video-to-ipad-videos-app – Vizllx Feb 09 '16 at 06:51

2 Answers2

2

Check this below methods:

  -(void)ButtonAction:(id)sender
    {
        NSURL *video = [NSURL URLWithString:YOURURL];
        NSData *data = [NSData  dataWithContentsOfURL:video];
        NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/myvideo_%@.mp4",docDirPath,CURRENT_TIMESTAMP];
        [data writeToFile:filePath atomically:YES];

        NSURL *urlPath  = [[NSURL alloc] initFileURLWithPath:filePath];
        [self saveToCameraRoll:urlPath dict:(NSDictionary *)sender];
    }

    -(void) saveToCameraRoll:(NSURL *)srcURL dict:(NSDictionary *)dictValues
    {
        NSLog(@"srcURL: %@", srcURL);

        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeVideoAtPathToSavedPhotosAlbum:srcURL completionBlock:^(NSURL *assetURL, NSError *error)
         {
             NSURL *savedAssetURL = assetURL;

             NSLog(@"asset url %@",assetURL);
             if(error)
             {
                error handling
             }
             else
             {
                 [library assetForURL:savedAssetURL resultBlock:^(ALAsset * alAsset)
                  {
                             NSLog(@"Saved");


                  }failureBlock:^(NSError *error)
                  {
                               NSLog(@"Not Saved");


                  }];
             }
         }];
    }
  • I am still getting Asset Url as null.Any idea,what could be the reason. srcURL is returning value but asset value is null – leni Feb 09 '16 at 10:17
  • once check the filepath, then send to saveTocamerRoll method if file path is exist. i have done this, its working fine for me. – Bharath Raj Feb 09 '16 at 10:23
  • i have tried the path exists.Still its not being saved to album and asset url is null – leni Feb 09 '16 at 10:28
  • the asset is showing as null and its not saving to gallery – leni Feb 10 '16 at 07:28
  • what is the alternative for ALAssetsLibrary to get the image name? ALAssetsLibrary is depreacted. I want to get the name of image that got selected –  Jun 09 '16 at 12:16
0

Try assetURL.absoluteString in completionBlock

Feng Liu
  • 954
  • 13
  • 22