使用Photos框架从图库上传视频 [英] Upload videos from gallery using Photos framework
问题描述
使用Photos框架从图库上传视频的最佳方法是什么?
之前我使用ALAssetRepresentation和下一个方法:
What is the best way to upload videos from gallery using Photos framework? Before I used ALAssetRepresentation and next method:
- (NSUInteger)getBytes:(uint8_t *)buffer fromOffset:(long long)offset length:(NSUInteger)length error:(NSError **)error;
这允许上传文件而不先将其复制到app临时目录。在Photos框架中看不到任何替代方案。唯一的方法似乎是使用AVAssetExportSession - >导出到本地目录 - >上传,但这需要额外的存储空间(如果视频太大可能有问题)
this allowed to upload file without first copying it to app temp directory. Don’t see any alternatives in Photos framework. Only way seems to use AVAssetExportSession -> export to local directory -> upload, but this requires additional storage space (could be a problem, if video is too big)
推荐答案
似乎唯一有效的方法是从 PHImageManager
请求 AVAsset
,并检查是否返回的资产是 AVURLAsset
。在这种情况下,URL可用于直接访问文件并获取所需的字节块:
Seems the only valid way is to request AVAsset
from PHImageManager
, and check if returned asset is AVURLAsset
. In this case URL can be used to directly access file and get needed chunk of bytes:
[[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]]) {
NSURL *URL = [(AVURLAsset *)asset URL];
// use URL to get file content
}
}];
这不适用于慢动作视频,因为 AVComposition $ c返回$ c>而不是
AVURLAsset
。可能的解决方案是使用 PHVideoRequestOptionsVersionOriginal
视频文件版本:
This will not work with slow motion videos, because AVComposition
instead of AVURLAsset
is returned. Possible solution is to use PHVideoRequestOptionsVersionOriginal
video file version:
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
[[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]]) {
NSURL *URL = [(AVURLAsset *)asset URL];
// use URL to get file content
}
}];
并获得全尺寸图片网址:
And to get fullsize image url:
PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
options.canHandleAdjustmentData = ^BOOL(PHAdjustmentData *adjustmentData) {
return YES;
};
[imageAsset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
// use contentEditingInput.fullSizeImageURL
}];
这篇关于使用Photos框架从图库上传视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!