2012-04-03 73 views
4

以下代碼是將從相機拍攝的圖像保存到相冊中。如何將錄製的視頻保存到相冊中?

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

如何將重新編碼的視頻保存爲照片相冊?

+2

[UISaveVideoAtPathToSavedPhotosAlbum](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UISaveVideoAtPathToSavedPhotosAlbum) – Hemang 2012-04-03 10:36:05

回答

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

} 
+0

我正在錄製視頻。不從任何文件取 – Shreedhar 2012-04-03 10:58:04

+0

你在哪裏使用目的地路徑 – Shreedhar 2012-04-03 11:01:30

+0

我編輯了這篇文章。現在請看看。 – 2012-04-03 11:28:20

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

這是蘋果示例rosywriter的代碼片段。 Sould工作。

 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文件中。

相關問題