ios - 使 requestAVAssetForVideo 同步
<p><p>我正在从图库中所有选定的视频中获取 URL,效果很好。</p>
<p>但我注意到 <code>requestAVAssetForVideo</code> 是以 <strong>Asynchronous</strong> 方式执行的。</p>
<p>之前我通过 <code>options:option</code> 更改 <code>options:nil</code> 指定 <code>requestImageForAsset</code> 以 <strong>Synchronous</strong> 方式执行></p>
<pre><code>PHImageRequestOptions *option = ;
option.synchronous = YES;
</code></pre>
<p>这就像一个魅力,但它与 <code>requestAVAssetForVideo</code></p> 的形式不同
<p>有没有办法以<strong>同步</strong>的方式执行<code>requestAVAssetForVideo</code>?</p>
<pre><code>[ requestAVAssetForVideo:lAsset
options:nil
resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
NSURL *url = (NSURL *)[[(AVURLAsset *)avAsset URL] fileReferenceURL];
NSLog(@"url = %@", );
NSLog(@"url = %@", );
}];
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>好吧,正如 CodeBender 所说,没有,但是,这是一种构建同步版本的方法:</p>
<pre><code>dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
PHVideoRequestOptions *option = ;
__block AVAsset *resultAsset;
[ 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
;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 使 requestAVAssetForVideo 同步,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/41007271/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/41007271/
</a>
</p>
页:
[1]