Saving video after tapping use in UIImagePickerController
I am have an app that calls UIImagePickerController
, and you take video using the controller. After you take the video, I want the video to be saved into the camera roll after clicking use in the 开发者_如何学编程UIImagePickerController
.
Can you help me?
Oh, and UIImagePickerController
comes with the option to switch to video, default photo. I want this to be only video, not photo.
My current code is :
picker.sourceType=UIImagePickerControllerCameraCaptureModeVideo;
picker.mediaTypes=[UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
Once you have implemented the following delegate method, pressing the 'Use' button will save your recorded video to the camera roll.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSURL *recordedVideoURL= [info objectForKey:UIImagePickerControllerMediaURL];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:recordedVideoURL]) {
[library writeVideoAtPathToSavedPhotosAlbum:recordedVideoURL
completionBlock:^(NSURL *assetURL, NSError *error){}
];
}
[library release];
}
Use the following to set the capture mode to video :
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
Here is the complete code that I use to launch the image picker for video recording :
UIImagePickerController * imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.showsCameraControls = YES;
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
imagePickerController.delegate = self;
imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
[self presentModalViewController:imagePickerController animated:YES];
}
I was trying to save the video using the absoluteURL of the URL but it didn't work
NSURL *mediaURL = [info objectForKey: UIImagePickerControllerMediaURL];
UISaveVideoAtPathToSavedPhotosAlbum(mediaURL.absoluteString, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
It finally worked using the path property:
UISaveVideoAtPathToSavedPhotosAlbum([mediaURL path], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
Hope it helps anyone.
From the UIImagePickerControllerDelegate
class reference:
To save a still image to the user’s Camera Roll album, call the
UIImageWriteToSavedPhotosAlbum
function from within the body of theimagePickerController:didFinishPickingMediaWithInfo:
method. To save a movie to the user’s Camera Roll album, instead call theUISaveVideoAtPathToSavedPhotosAlbum
function. These functions, described in UIKit Function Reference, save the image or movie only; they do not save metadata.
Your UIImagePickerControllerDelegate may look something like this...
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:@"mediaType"];
if (![mediaType isEqualToString:kUTTypeMovie])
return;
NSURL *mediaURL = [info objectForKey:@"mediaURL"];
UISaveVideoAtPathToSavedPhotosAlbum(mediaURL.absoluteString, self, @selector(video:didFinishSavingWithError:contextInfo:), NULL);
}
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo
{
if (!videoPath && error)
{
NSLog(@"Error saving video to saved photos roll: %@, %@", error, [error userInfo]);
// Handle error;
return;
}
// Video was saved properly. UI may need to be updated here.
}
Notice that I've implemented the completion selector for UISaveVideoToSavedPhotosAlbum()
. If it were to fail for any reason, the error would be reported here and you can then handle it appropriately.
精彩评论