Search code examples
objective-cios9video-captureavcapturesessionalassetslibrary

How to Save Recorded video to camera roll?


I am trying to record video with iPhone camera and saving to camera roll following this blog. However, using AVCaptureSession ,camera gets initialized. But when try to save video to camera roll, it gives URL error. Here's my code snippet

NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"myApp.mov"];

            NSURL *outputURL = [NSURL URLWithString:outputPath];
          //  NSLog(@"Output URL  %@ AND ABSOLUTE STRING %@",outputURL,outputURL.absoluteString);
            NSFileManager *fileManager = [NSFileManager defaultManager];
            if ([fileManager fileExistsAtPath:outputPath])
            {
                NSError *error;
                if ([fileManager removeItemAtPath:outputPath error:&error] == NO)
                {
                    //Error - handle if requried
                }
            }
            //Start recording
            [MovieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];

It gives the below error

[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] - Cannot record to URL /private/var/mobile/Containers/Data/Application/FEA51E75-B2A6-45DD-991B-AFFBF5794EAC/tmp/myApp.mov because it is not a file URL.

But if I change URL to initFileURLWithPath, it gives another error.

Video /private/var/mobile/Containers/Data/Application/B6876493-4354-4607-B348-63C5262AF2D9/tmp/myApp.mov cannot be saved to the saved photos album: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSErrorFailingURLStringKey=file:///private/var/mobile/Containers/Data/Application/B6876493-4354-4607-B348-63C5262AF2D9/tmp/myApp.mov, NSErrorFailingURLKey=file:///private/var/mobile/Containers/Data/Application/B6876493-4354-4607-B348-63C5262AF2D9/tmp/myApp.mov, NSLocalizedDescription=The requested URL was not found on this server., NSUnderlyingError=0x171ad710 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}, NSURL=file:///private/var/mobile/Containers/Data/Application/B6876493-4354-4607-B348-63C5262AF2D9/tmp/myApp.mov}

I really don't understand the error. Can anyone please help me sorting this out ?


Solution

  • Did you tried like this:

       NSString *outputFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[@"myApp" stringByAppendingPathExtension:@"mov"]];
    

    Ref : same example like you are working, https://stackoverflow.com/a/21118720/4557505

    else

    NSString *rootPath = NSTemporaryDirectory();
    NSString  *videoPath = [rootPath stringByAppendingPathComponent:@"myApp.mov"];
    NSURL *outputURL = [NSURL fileURLWithPath:videoPath];
    

    The above code works for me in copyItemAtURL