我怎样才能上传视频使用photokit?

时间:2015-08-21 07:27:56

标签: objective-c video photokit

在iOS8之前,我使用ALAsset并使用缓冲区获取NSData然后上传视频,如下所示;

Byte * buffer = (Byte *)malloc(lenght);

NSError * error = nil;

NSUInteger readLength = [self.asset.defaultRepresentation getBytes:buffer fromOffset:self.sendedSize length:lenght error:&error];

NSData * data = [NSData dataWithBytes:buffer length:readLength];

那么,我如何上传视频使用PhotoKit?我怎样才能获得视频数据?

1 个答案:

答案 0 :(得分:3)

这个怎么样:

    PHAsset *videoAsset /*Your video asset */;
    PHVideoRequestOptions *options = /*Options if you need them */;
    [[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
        NSURL *fileURL = /*Create a temporary file URL*/;
        exportSession.outputURL = fileURL;

        [exportSession exportAsynchronouslyWithCompletionHandler:^{
            NSData *assetData = [NSData dataWithContentsOfURL:fileURL];
            /* Do whatever you want to do with this data, and delete it afterwards */      
        }];
    }];