我从图库中所有选定的视频中获取 URL,效果很好。
但我注意到 requestAVAssetForVideo
是以异步方式执行的。
之前我通过 options:option
更改 options:nil
指定以同步方式执行 requestImageForAsset
>
PHImageRequestOptions *option = [PHImageRequestOptions new];
option.synchronous = YES;
这就像一个魅力,但它与 requestAVAssetForVideo
的形式不同
有没有办法以同步方式执行requestAVAssetForVideo
?
[[PHImageManager defaultManager] requestAVAssetForVideo:lAsset
options:nil
resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
NSURL *url = (NSURL *)[[(AVURLAsset *)avAsset URL] fileReferenceURL];
NSLog(@"url = %@", [url absoluteString]);
NSLog(@"url = %@", [url relativePath]);
}];
最佳答案
嗯,正如 CodeBender 所说,没有,但是,这是构建同步版本的一种方法:
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
PHVideoRequestOptions *option = [PHVideoRequestOptions new];
__block AVAsset *resultAsset;
[[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset
options:option
resultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info)
{
resultAsset = avasset;
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
// We synchronously have the asset
[self doSomethingWithAsset:resultAsset];
关于ios - 使 requestAVAssetForVideo 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41007271/