如何将录制的视频保存到相册中?

时间:2012-04-03 10:21:28

标签: iphone ios xcode

以下代码是将图像从相机保存到相册中。

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
    {
         image = [info objectForKey:UIImagePickerControllerEditedImage];
        UIImageWriteToSavedPhotosAlbum(image, self,
                                       @selector(image:finishedSavingWithError:contextInfo:),
                                       nil);
    }

如何将重新编码的视频保存到photoAlbum?

3 个答案:

答案 0 :(得分:19)

检查以下代码......

   - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{



    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
       if ([mediaType isEqualToString:@"public.movie"]){        
               // Saving the video / // Get the new unique filename 
               NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath]; 
                  UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);

}

答案 1 :(得分:1)

- (void)saveMovieToCameraRoll
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:movieURL
                                completionBlock:^(NSURL     *assetURL, NSError *error) {
                                    if (error)
                                        [self     showError:error];
                                    else
                                        [self     removeFile:movieURL];

                                        dispatch_async(movieWritingQueue, ^{
                                            recordingWillBeStopped = NO;
                                            self.recording = NO;

                                            [self.delegate recordingDidStop];
                                    });
                                }];
    [library release];
}

这是Apple示例rosywriter的代码片段。可以工作。

        movieURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), @"Movie.MOV"]];
        [movieURL retain];

以上行创建视频的文件和路径。

- (void) startRecording
{
    dispatch_async(movieWritingQueue, ^{

        if ( recordingWillBeStarted || self.recording )
            return;

        recordingWillBeStarted = YES;

        // recordingDidStart is called from     captureOutput:didOutputSampleBuffer:fromConnection: once the asset writer is setup
        [self.delegate recordingWillStart];

        // Remove the file if one with the same name already exists
        [self removeFile:movieURL];

        // Create an asset writer
        NSError *error;
        assetWriter = [[AVAssetWriter alloc] initWithURL:movieURL fileType:(NSString *)kUTTypeQuickTimeMovie error:&error];
        if (error)
            [self showError:error];
    }); 
}

此功能用于使用avassetwriter开始将视频录制到该movieURL文件中。

答案 2 :(得分:0)

尝试以下代码

- (void)saveVideo:(NSURL *)videoUrl {
    NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];
    [videoData writeToFile:@"YOUR_PATH_HERE" atomically:YES];
  }    

  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
      NSString *type = [mediaDict objectForKey:UIImagePickerControllerMediaType];

      if ([type isEqualToString:(NSString *)kUTTypeVideo] || 
          [type isEqualToString:(NSString *)kUTTypeMovie]) { // movie != video
          NSURL *videoURL [mediaDict objectForKey:UIImagePickerControllerMediaURL];
          [self saveVideo:videoUrl];
      }

  }

或者你也可以尝试一下 将视频保存到文档目录如下

   - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


  NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

  NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"];

  BOOL success = [videoData writeToFile:tempPath atomically:NO];


  [picker dismissModalViewControllerAnimated:YES];
  }