Editing Videos on an ios Device(编辑手机上的视频)

本文介绍了一个iOS应用如何从照片库中选择视频,并使用UIImagePickerController进行媒体选择。随后通过UIVideoEditorController提供视频编辑功能,包括视频裁剪等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值