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

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

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 桌面上,当你长按一个应用程序图标时,应用程序会开始抖动并显示一个“X”按钮,以便你可以删除应用程序或将其移动到新位置。下面是如何为 iOS 桌面编辑添加抖动动画的步骤: 1. 创建编辑模式 首先,你需要创建一个编辑模式,该模式将允许用户在屏幕上移动应用程序图标和删除应用程序。可以使用以下代码创建编辑模式: ```swift func setEditing(_ editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) collectionView?.allowsMultipleSelection = editing let indexPaths = collectionView?.indexPathsForVisibleItems ?? [] for indexPath in indexPaths { let cell = collectionView?.cellForItem(at: indexPath) as? AppIconCell cell?.isEditing = editing } } ``` 2. 添加抖动动画 要为编辑模式添加抖动动画,可以使用以下代码: ```swift private func animateShake(for cells: [UICollectionViewCell]) { for cell in cells { let shakeAnimation = CABasicAnimation(keyPath: "position") shakeAnimation.duration = 0.08 shakeAnimation.repeatCount = 2 shakeAnimation.autoreverses = true shakeAnimation.fromValue = NSValue(cgPoint: CGPoint(x: cell.center.x - 5, y: cell.center.y)) shakeAnimation.toValue = NSValue(cgPoint: CGPoint(x: cell.center.x + 5, y: cell.center.y)) cell.layer.add(shakeAnimation, forKey: "position") } } ``` 该函数将遍历所有应用程序图标单元格,并为每个单元格添加一个基本的抖动动画。你可以自定义抖动动画的属性,例如持续时间、重复计数和偏移量。 3. 触发抖动动画 最后,在编辑模式下,你需要在用户长按应用程序图标时触发抖动动画。可以使用以下代码实现: ```swift override func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) { super.collectionView(collectionView, didHighlightItemAt: indexPath) if isEditing { let cells = collectionView.visibleCells animateShake(for: cells) } } ``` 该函数将检查编辑模式是否处于活动状态。如果是,则遍历所有应用程序图标单元格并为每个单元格添加抖动动画。 希望这可以帮助你添加 iOS 桌面编辑的抖动动画!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值