1。导入库:
#import<MobileCoreServices/MobileCoreServices.h>
#import<AssetsLibrary/AssetsLibrary.h>
实现协议: <UINavigationControllerDelegate, UIVideoEditorControllerDelegate, UIImagePickerControllerDelegate>
e.g.
/* 解决思路:pick a video from the photo library
then display a video editor controller
then allow the user to edit the video picked
*/@property (nonatomic, strong) NSURL *videoURLToEdit;
@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
- (BOOL) isPhotoLibraryAvailable{
return[UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary];
}
- (BOOL) cameraSupportsMedia:(NSString *)paramMediaType
sourceType:(UIImagePickerControllerSourceType)paramSourceType{
__block BOOL result = NO;
if ([paramMediaType length] == 0){
NSLog(@"Media type is empty.");
return NO;
}
NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:paramSourceType];
[availableMediaTypes enumerateObjectsUsingBlock:
^(id obj, NSUInteger idx, BOOL *stop) {
NSString *mediaType = (NSString *)obj;
if ([mediaType isEqualToString:paramMediaType]){
result = YES;
*stop= YES;
}
}];
return result;
}
- (BOOL) canUserPickVideosFromPhotoLibrary{
return [self cameraSupportsMedia:(__bridge NSString *)kUTTypeMovie
sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (void)actionPick{
self.assetsLibrary = [[ALAssetsLibrary alloc] init];
if ([self isPhotoLibraryAvailable] && [self canUserPickVideosFromPhotoLibrary]){
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // Set the source type to photo library
imagePicker.mediaTypes = @[(__bridge NSString *)kUTTypeMovie]; // pick movies from the library
imagePicker.delegate = self;
//注意,后续必须先dismiss才能再present另一个
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
// UIImagePickerControllerDelegate
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"Picker returned successfully.");
NSString *mediaType =[info objectForKey: UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){
self.videoURLToEdit =[info objectForKey:UIImagePickerControllerMediaURL];
}
//dismissViewController <->presentViewController 注意,必须先dismiss之前的present,然后才能present另一个,否则会出错
[picker dismissViewControllerAnimated:YES completion:^{
if (self.videoURLToEdit != nil){
NSString *videoPath = [self.videoURLToEdit path];
/* 此路径是否可编辑 */
if ([UIVideoEditorController canEditVideoAtPath:videoPath]){
UIVideoEditorController *videoEditor =[[UIVideoEditorController alloc] init];
videoEditor.delegate = self;
videoEditor.videoPath = videoPath;
[self presentViewController:videoEditor animated:YES completion:nil];
self.videoURLToEdit = nil;
} else {
NSLog(@"Cannot edit the video at this path");
}
}
}];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"Picker was cancelled");
self.videoURLToEdit = nil;
[picker dismissViewControllerAnimated:YES completion:nil];
}
// UIVideoEditorControllerDelegate- (void)videoEditorController:(UIVideoEditorController *)editor
didSaveEditedVideoToPath:(NSString *)editedVideoPath{
NSLog(@"The video editor finished saving video");
NSLog(@"The edited video path is at = %@", editedVideoPath);
[editor dismissViewControllerAnimated:YES completion:nil];
}
- (void)videoEditorController:(UIVideoEditorController *)editor
didFailWithError:(NSError *)error{
NSLog(@"Video editor error occurred = %@", error);
[editor dismissViewControllerAnimated:YES completion:nil];
}
- (void)videoEditorControllerDidCancel:(UIVideoEditorController *)editor{
NSLog(@"The video editor was cancelled");
[editor dismissViewControllerAnimated:YES completion:nil];
}