iOS 从相册导出视频到沙盒

第一种

UIImagePickerController

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info 
{
    [self dismissViewControllerAnimated:YES completion:nil];

    NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
    NSData *video = [NSData dataWithContentsOfURL:url];
    if (video.length/1024/1024 >100) { // video size is more than 100 M 
        // code goes here 
        return;
    }

    NSURL *videoSaveURL = kVideoSavePath;
    AVAsset *asset = [AVAsset assetWithURL:url];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
    exportSession.outputURL = videoSaveURL;
    exportSession.outputFileType = AVFileTypeMPEG4; // mp4
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        int exportState = exportSession.status;
        switch (exportState) {
            case AVAssetExportSessionStatusFailed: // export failed
                // code goes here
                break;
            case AVAssetExportSessionStatusCompleted: // finish
            {
                // first frame of video
                #if 1
                AVAssetImageGenerator *imgeGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
                imgeGenerator.appliesPreferredTrackTransform = YES;
                CMTime time = CMTimeMakeWithSeconds(0.0, 600);
                NSValue* timeValue = [NSValue valueWithCMTime:time];
                [imgeGenerator generateCGImagesAsynchronouslyForTimes:@[timeValue] completionHandler:^(CMTime requestedTime, CGImageRef  _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
                    // image
                    UIImage *newImage = [UIImage imageWithCGImage:image];
                    // video in sandbox at 'kVideoSavePath'
                    // code...
                }];
                #else
                    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
                    generator.appliesPreferredTrackTransform = YES;
                    NSError *error;
                    CGImageRef imageRef = [generator copyCGImageAtTime:CMTimeMake(0, 10) actualTime:NULL error:&error];
                    if (error) {
                        return nil;
                    }
                    UIImage *image = [UIImage imageWithCGImage:imageRef];
                #endif
            }
                break;
            default:
                break;
        }
    }];
}

第二种

PHAsset

    // Option
    PHVideoRequestOptions *option = [[PHVideoRequestOptions alloc] init];
    option.version = PHVideoRequestOptionsVersionCurrent; // default
    option.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic; // default

    // Manager
    PHImageManager *manager = [PHImageManager defaultManager];
    [manager requestExportSessionForVideo:asset options:option exportPreset:AVAssetExportPresetMediumQuality  resultHandler:^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) {

        // Path
        NSString *videoPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"saved.mp4"];
        NSLog(@"videoPath:%@",videoPath);

        // Export
        exportSession.outputURL = [NSURL fileURLWithPath:videoPath];
        exportSession.shouldOptimizeForNetworkUse = NO;
        exportSession.outputFileType = AVFileTypeMPEG4; // mp4
        [exportSession exportAsynchronouslyWithCompletionHandler:^{
            switch ([exportSession status]) {
                case AVAssetExportSessionStatusFailed:{ 
                    // code...
                }break;
                case AVAssetExportSessionStatusCompleted:{
                    // code...
                }break;
                default:
                    break;
            }
        }];
    }];
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
iOS和安卓的盒是用来保护应用程序数据的安全性的一种机制。它们的主要区别如下: 1. 安全性:iOS盒机制相对安卓更加严格。iOS要求应用程序在自己的盒内运行,限制了应用之间的直接访问和交互。而安卓的盒机制相对较弱,应用程序可以更容易地访问和共享数据。 2. 文件系统结构:iOS盒机制将每个应用程序的文件分隔为多个目录,包括应用程序包、文档目录、缓存目录等。每个目录只能由特定的应用程序进行访问和写入。而安卓的盒机制通常是基于应用的用户ID,并且应用程序可以自由地向盒中的文件系统写入和访问。 3. 权限管理:iOS盒机制通过权限管理来限制应用程序对系统资源的访问。应用程序需要在安装时声明所需的访问权限,并由用户在使用时授予。而安卓的盒机制在应用程序安装时会一次性获取所有权限,并且用户只能在软件设置中进行修改。 4. 应用程序的更新:iOS盒机制要求每个应用程序都在单独的容器中运行,因此应用程序的更新通常是通过替换整个应用程序包来完成的。而安卓的盒机制允许应用程序在更新时只替换其中的部分文件,从而减少下载和更新时间。 综上所述,iOS和安卓的盒机制在安全性、文件系统结构、权限管理和应用程序更新方面存在一些差异。iOS盒机制相对更严格,保护了应用程序和用户数据的安全性,但也可能限制了应用程序之间的交互。而安卓的盒机制相对较弱,允许应用程序更自由地访问和共享数据,但也可能增加了安全隐患。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豪冷啊

你的鼓励是对我的认可!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值