iOS saving video back to photo album

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
I have an app in which the viewer can either open the camera to take a video or they can choose a video from the photoLibrary. I use two different actions and buttons to do this using UIImagePickerController.

Code:
//get it from the camera
-(IBAction) captureMovie:(id)sender;
{  UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType =  UIImagePickerControllerSourceTypeCamera;
 NSArray *mediaTypesAllowed = [NSArray arrayWithObject:@"public.movie"];
picker.mediaTypes = mediaTypesAllowed;
picker.delegate = self;   
[self presentModalViewController: picker animated:YES];
}
//get it from the photoLibrary
-(IBAction) findMovie:(id)sender;
{  UIImagePickerController *picker2 = [[UIImagePickerController alloc] init];
picker2.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
NSArray *mediaTypesAllowed2 = [NSArray arrayWithObject:@"public.movie"];
picker2.mediaTypes = mediaTypesAllowed2;
picker2.delegate = self; 
[self presentModalViewController: picker2 animated:YES];
}
I then save each one to the documents directory with a new name and I also save the video from the camera to the photoLibrary.

Code:
- (void) imagePickerController:(UIImagePickerController *)picker    didFinishPickingMediaWithInfo:(NSDictionary *)info
{ NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;// Get the URL of where the movie is stored.
NSData * movieData = [NSData dataWithContentsOfURL:movieURL];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
//here I save the camera video to the documents directory
[ movieData writeToFile:fullPath atomically:YES];
//and here I save it to the photo album 
UISaveVideoAtPathToSavedPhotosAlbum(fullPath, nil, nil, nil) ;

- (void) imagePickerController1:(UIImagePickerController *)picker2 didFinishPickingMediaWithInfo:(NSDictionary *)info2
{ NSURL * movieURL2 = [info2 valueForKey:UIImagePickerControllerMediaURL] ;
NSData * movieData2 = [NSData dataWithContentsOfURL:movieURL2];
NSArray *documentPaths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
//here I save the photoLibrary video to the documents directory
[ movieData2 writeToFile:fullPath2 atomically:YES]; 
   //I do not save it to the photo album
I don't code saving the video chosen from the photoLibrary back to the photo album but somehow it is. If I remove the code that saves the captured video to the photo album then the chosen video is not saved back to the photo album.

How do I capture a video using the camera and save it to the photo album but NOT save a video chosen from the photoLibrary back to the photo album. Any Ideas would be greatly appreciated.
 

xStep

macrumors 68020
Jan 28, 2003
2,013
99
Less lost in L.A.
I don't see how that last method can get called since it doesn't exist as a UIImagePickerControllerDelegate. Only the third one looks valid.

Add a BOOL variable to your class and set it to YES when you use the camera and NO when your read a file from the photo album. Then in the valid delegate take a path of code depending on that value.

Appropriately set that BOOL variable in the first line of your two setup methods.
 
Comment

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
saving video back to photo album

That did the trick. Everything is working fine there. Thanks!!
 
Comment

gbenna

macrumors member
Original poster
Jul 27, 2011
62
0
movie won't play upon entering view

I have an app in which the viewer can record some video in one view, name it, and save it to the documents directory. They then exit that view using the tab bar and choose a cell in a tableview. This loads a flow view which contains a bunch of pictures. When one picture is double tapped the video just taken (or others previously taken) is supposed to start playing.
The problem is the video doesn't start playing. If the user enters another flow view from a different cell in the tableview and then double taps on one of theses images and then returns to the original recorded video it will then play.
I'm sure I have set all everything to nil in my record video view (I don't know if this should make a difference of not).

This is my code for the capture and saving of the movie.


Code:
-(IBAction) captureMovie:(id)sender;
{  UIImagePickerController *picker = [[UIImagePickerController alloc] init];
     if( (UIButton *)sender == takeMovie ){
         picker.sourceType =  UIImagePickerControllerSourceTypeCamera;
    }
    else if ((UIButton *)sender == getMovie) {
        picker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
    }
     NSArray *mediaTypesAllowed = [NSArray arrayWithObject:@"public.movie"];
    picker.mediaTypes = mediaTypesAllowed;
    picker.delegate = self;   
    [self presentModalViewController: picker animated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)  picker
{
    [picker dismissModalViewControllerAnimated:YES];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
        NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
        NSData * movieData = [NSData dataWithContentsOfURL:movieURL];
        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [documentPaths objectAtIndex:0];
        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[[self imageNameTextField]text]];
        fullPath = [fullPath stringByAppendingFormat:@".MOV"];
        [ movieData writeToFile:fullPath atomically:YES]; 

    }

and this it the code in the view which loads and plays the movie.

Code:
-(void) viewWillAppear:(BOOL)animated{
    
    NSString *audioName = [myDictionary objectForKey:@"photoVideokey"];
    NSArray *pathsa = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectorya = [pathsa objectAtIndex:0];
    //Get a full path to the image in the documents directory.
    NSString *fullPatha = [documentsDirectorya stringByAppendingPathComponent:audioName];
    NSURL *url = [NSURL fileURLWithPath:fullPatha];
    moviePlayer =[[MPMoviePlayerController alloc] initWithContentURL: url];
      // frame must match parent view
    [self.view addSubview: [moviePlayer view]];
    [[moviePlayer view] setFrame:[self.view bounds]];
    [moviePlayer prepareToPlay];
    [moviePlayer play];
    NSLog (@"%@",url);
    
}
If anyone can think of why it should be acting this way please give me some suggestions.
 
Comment

dejo

Moderator
Staff member
Sep 2, 2004
15,981
450
The Centennial State
Mod Note: Your new thread was a follow-up to your previous thread. Therefore it has been merged with it. Please do not create another new thread on this topic.
 
Comment

Similar threads

Replies
28
Views
3K
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.