2012-04-03 4 views
4

Le code suivant permet d'enregistrer l'image prise de la caméra dans un album photo.Comment enregistrer une vidéo enregistrée dans un album photo?

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

Comment enregistrer une vidéo recodée dans photoAlbum?

+2

[UISaveVideoAtPathToSavedPhotosAlbum] (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UISaveVideoAtPathToSavedPhotosAlbum) – Hemang

Répondre

19

Consultez ci-dessous le code ...

- (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

i am Enregistrement vidéo . ne pas prendre à partir de n'importe quel fichier – Shreedhar

+0

où vous utilisez le chemin de destination – Shreedhar

+0

J'ai édité le post.S'il vous plaît regarder maintenant. –

0

Essayez ci-dessous le code

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

    } 

ou vous pouvez essayer cela aussi Enregistrement de la vidéo dans le répertoire des documents est la suivante

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

Ceci est l'extrait de code d'un exemple de rosywriter. Je devrais travailler.

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

Les lignes ci-dessus permettent de créer un fichier et un chemin pour la vidéo.

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

Cette fonction permet de lancer l'enregistrement vidéo dans ce fichier movieURL en utilisant avassetwriter.

Questions connexes