美文网首页iOS开发技术分享
获取PHAsset类的对象中视频资源/大小/七牛上传/进度监听

获取PHAsset类的对象中视频资源/大小/七牛上传/进度监听

作者: 喵子G | 来源:发表于2017-11-17 10:49 被阅读583次

    PHAsset类的对象对应的视频并不一定保存在本地相册,也可能保存在云相册,本地只是有一个缩略图,需要联网同步:

    // 允许同步网络相册
    PHVideoRequestOptions *option = [[PHVideoRequestOptions alloc] init];
    option.networkAccessAllowed = YES;
    option.progressHandler = ^(double progress, NSError *error, BOOL *stop, NSDictionary *info) {
        if (error) {
            weakSelf.progressView.hidden = YES;
            weakSelf.videoImageView.image = preImage;
            [SVProgressHUD showErrorWithStatus:@"相册同步资源失败"];
        }
    };
    // 获取视频数据
    [[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset options:option resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
        if ([asset isKindOfClass:[AVURLAsset class]]) {
            AVURLAsset* urlAsset = (AVURLAsset*)asset;
            NSNumber *size;
            [urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
            // 视频大小
            CGFloat videoSize = [size floatValue]/(1024.0*1024.0);
            if (videoSize > 12.00) {
                weakSelf.progressView.hidden = YES;
                weakSelf.videoImageView.image = preImage;
                [SVProgressHUD showErrorWithStatus:@"上传视频不能大于12M"];
            } else {
                // 视频数据
                NSData *data = [NSData dataWithContentsOfURL:urlAsset.URL];
                [[JKRQiNiuManager sharedManager] uploadVideo:data progressHandler:^(NSString *key, float percent) {
                    NSLog(@"%@-%f", key, percent);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        weakSelf.progressView.progress = percent;
                    });
                } success:^(NSString * _Nonnull url) {
                    weakSelf.progressView.hidden = YES;
                } failure:^{
                    weakSelf.progressView.hidden = YES;
                    weakSelf.videoImageView.image = preImage;
                    [SVProgressHUD showErrorWithStatus:@"上传视频失败"];
                }];
            }
        }
    }];
    

    七牛上传

    QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
        progress(key, percent);
    } params:nil checkCrc:NO cancellationSignal:^BOOL{
        return self.flag;
    }];
    [self.qnUploadManager putData:video key:@"filename" token:@"token" complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        if (info.ok) {
            NSString *vidoURL = @"";
            success(vidoURL);
        } else {
            failure();
        }
    } option:uploadOption];
    

    相关文章

      网友评论

        本文标题:获取PHAsset类的对象中视频资源/大小/七牛上传/进度监听

        本文链接:https://www.haomeiwen.com/subject/axwdvxtx.html