如何使用 photokit 上传视频?
how can I upload video use 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 上传视频?如何获取视频数据?
这个怎么样:
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 */
}];
}];
在 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 上传视频?如何获取视频数据?
这个怎么样:
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 */
}];
}];