如果我使用“dispatch_semaphore_wait”块永远不会执行

时间:2017-01-10 03:15:13

标签: ios grand-central-dispatch semaphore video-processing

我正在尝试使用信号量  但是如果我使用dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); AVVideoComposition *videoComposition = [AVVideoComposition videoCompositionWithAsset:self.selectedVideo applyingCIFiltersWithHandler:^(AVAsynchronousCIImageFilteringRequest * _Nonnull request) { CIImage *filteredImage = [CIImage CIFilterCustomLUTFilter:request.sourceImage CustomLUTFilter:self.filterArray[indexPath.row]]; [request finishWithImage:filteredImage context:nil]; NSLog(@"signal here %@", [NSThread currentThread]); dispatch_semaphore_signal(semaphore); }]; NSLog(@"wait here %@", [NSThread currentThread]); dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); self.playerLayer.player.currentItem.videoComposition = videoComposition; AVAssetExportSession *export = [AVAssetExportSession exportSessionWithAsset:self.selectedVideo presetName:AVAssetExportPresetHighestQuality]; export.outputFileType = AVFileTypeQuickTimeMovie; export.videoComposition = videoComposition; NSString *savingPath = [NSString stringWithFormat:@"%@/video.mov", NSTemporaryDirectory()]; export.outputURL = [NSURL fileURLWithPath:savingPath]; [export exportAsynchronouslyWithCompletionHandler:^{ UISaveVideoAtPathToSavedPhotosAlbum(savingPath, nil, nil, nil); }]; }); ,则永远不会执行  我检查了线程,绝对等待线程和信令线程是不同的线程。
首先,我尝试了以下代码。

applyCIFilterWithHandler

我在这里尝试做的是在导出视频之前等待dispatch_semaphore_wait()的结果。 但通过这种方式,主线程被阻止,App陷入死锁状态。

第一个问题。我不能在主线程上使用dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); AVVideoComposition *videoComposition = [AVVideoComposition videoCompositionWithAsset:self.selectedVideo applyingCIFiltersWithHandler:^(AVAsynchronousCIImageFilteringRequest * _Nonnull request) { CIImage *filteredImage = [CIImage CIFilterCustomLUTFilter:request.sourceImage CustomLUTFilter:self.filterArray[indexPath.row]]; [request finishWithImage:filteredImage context:nil]; NSLog(@"signal here %@", [NSThread currentThread]); dispatch_semaphore_signal(semaphore); }]; dispatch_queue_t myQueue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT); dispatch_async(myQueue, ^{ NSLog(@"wait here %@", [NSThread currentThread]); dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); self.playerLayer.player.currentItem.videoComposition = videoComposition; AVAssetExportSession *export = [AVAssetExportSession exportSessionWithAsset:self.selectedVideo presetName:AVAssetExportPresetHighestQuality]; export.outputFileType = AVFileTypeQuickTimeMovie; export.videoComposition = videoComposition; NSString *savingPath = [NSString stringWithFormat:@"%@/video.mov", NSTemporaryDirectory()]; export.outputURL = [NSURL fileURLWithPath:savingPath]; [export exportAsynchronouslyWithCompletionHandler:^{ UISaveVideoAtPathToSavedPhotosAlbum(savingPath, nil, nil, nil); }]; }); 吗?即使等待的线程和信令线程彼此不同呢?

我尝试了以下代码后

dispatch_group

一切都是一样但我让我的自定义线程等待信号。但这一次,块永远不会被执行。虽然我的应用程序没有陷入僵局......(当然,这次我没有阻止主线程)

第二个问题。如何在这里正确实现信号量?

如果你不只是说'只使用dispatch_group来代替!,我将非常感激。当然,提供另一种选择来实现相同的东西是非常好的。那么,请给我两种方式来实现信号量和dispatch_group。

非常感谢!

另一次尝试

我尝试使用dispatch_group_notifyapplyCIFilterWithHandler实现此功能,但仍然永远不会调用and块。(当没有像dispatch_group,semaphore这样的东西时,它会被调用。

这是由1dispatch_group dispatch_group_t group = dispatch_group_create(); dispatch_group_enter(group); AVVideoComposition *videoComposition = [AVVideoComposition videoCompositionWithAsset:self.selectedVideo applyingCIFiltersWithHandler:^(AVAsynchronousCIImageFilteringRequest * _Nonnull request) { CIImage *filteredImage = [CIImage CIFilterCustomLUTFilter:request.sourceImage CustomLUTFilter:self.filterArray[indexPath.row]]; [request finishWithImage:filteredImage context:nil]; NSLog(@"block done here %@", [NSThread currentThread]); dispatch_group_leave(group); }]; dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"start saving here %@", [NSThread currentThread]); self.playerLayer.player.currentItem.videoComposition = videoComposition; AVAssetExportSession *export = [AVAssetExportSession exportSessionWithAsset:self.selectedVideo presetName:AVAssetExportPresetHighestQuality]; export.outputFileType = AVFileTypeQuickTimeMovie; export.videoComposition = videoComposition; NSString *savingPath = [NSString stringWithFormat:@"%@/video.mov", NSTemporaryDirectory()]; export.outputURL = [NSURL fileURLWithPath:savingPath]; [export exportAsynchronouslyWithCompletionHandler:^{ UISaveVideoAtPathToSavedPhotosAlbum(savingPath, nil, nil, nil); }]; }); dispatch_group_notify`实现的代码

{{1}}

0 个答案:

没有答案