【问题标题】:Scrolling UICollectionView blocks main thread滚动 UICollectionView 阻塞主线程
【发布时间】:2014-10-06 11:36:03
【问题描述】:

我有一个使用 AVSampleBufferDisplayLayer 播放 H264 的视频解码器,一切正常,直到我在同一个视图控制器上滚动 UICollectionViewController。这似乎阻塞了导致应用程序崩溃的主线程。我尝试使用 dispatch_async 将此代码放在单独队列中的一个块中,但仍然存在相同的阻塞问题以及解码器上的进一步性能问题。

dispatch_async(sampleQueue, ^{

                        [sampleBufferQueue addObject:(__bridge id)(sampleBuffer)];

                        if ([avLayer isReadyForMoreMediaData]) {
                            CMSampleBufferRef buffer = (__bridge CMSampleBufferRef)([sampleBufferQueue objectAtIndex:0]);
                            [sampleBufferQueue removeObjectAtIndex:0];
                            [avLayer enqueueSampleBuffer:buffer];
                            buffer = NULL;

                            NSLog(@"I Frame");
                            [avLayer setNeedsDisplay];
                            while ([sampleBufferQueue count] > 0 && [avLayer isReadyForMoreMediaData]) {

                                CMSampleBufferRef buffer = (__bridge CMSampleBufferRef)([sampleBufferQueue objectAtIndex:0]);
                                [sampleBufferQueue removeObjectAtIndex:0];
                                [avLayer enqueueSampleBuffer:buffer];
                                buffer = NULL;
                                NSLog(@"I Frame from buffer");
                                [avLayer setNeedsDisplay];
                            }
                        }
                        else {
                            NSLog(@"AVlayer Not Accepting Data (I)");
                        }
                    });

有没有办法将此任务优先于用户界面操作(如滚动集合视图等)?抱歉,我对 IOS 相当陌生。

【问题讨论】:

    标签: ios grand-central-dispatch


    【解决方案1】:

    原来 UICollectionView 在主线程上阻塞了对 NSURLConnection 的委托调用。这解决了问题:

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:self];
    

    改为

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:self
                                                      startImmediately:NO];
    [connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
                      forMode:NSRunLoopCommonModes];
    [connection start];
    

    【讨论】:

      猜你喜欢
      • 2018-02-16
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多