GPUImageView渲染慢问题

2020-09-08  本文已影响0人  开发者老岳

更新xocde11.4之后发现GPUImage第一次渲染的时候卡顿,如下信息:

Main Thread Checker: UI API called on a background thread: -[UIView bounds]
PID: 5821, TID: 1057983, Thread name: (none), Queue name: com.sunsetlakesoftware.GPUImage.openGLESContextQueue, QoS: 0
Backtrace:
4   GPUImageDemo                        0x0000000100fda790 __39-[GPUImageView recalculateViewGeometry]_block_invoke + 56
5   GPUImageDemo                        0x0000000100fb3fe0 runSynchronouslyOnVideoProcessingQueue + 108
6   GPUImageDemo                        0x0000000100fda738 -[GPUImageView recalculateViewGeometry] + 108
7   GPUImageDemo                        0x0000000100fdb134 __37-[GPUImageView setInputSize:atIndex:]_block_invoke + 312
8   GPUImageDemo                        0x0000000100fb3fe0 runSynchronouslyOnVideoProcessingQueue + 108
9   GPUImageDemo                        0x0000000100fdafdc -[GPUImageView setInputSize:atIndex:] + 136
10  GPUImageDemo                        0x0000000100f8d2ac -[GPUImageFilter informTargetsAboutNewFrameAtTime:] + 748
11  GPUImageDemo                        0x0000000100f8f34c -[GPUImageFilter newFrameReadyAtTime:atIndex:] + 208
12  GPUImageDemo                        0x0000000100fcfbf0 -[GPUImageTwoInputFilter newFrameReadyAtTime:atIndex:] + 1132
13  GPUImageDemo                        0x0000000100fb9078 __53-[GPUImagePicture processImageWithCompletionHandler:]_block_invoke + 644
14  libdispatch.dylib                   0x00000001018c205c _dispatch_call_block_and_release + 32
15  libdispatch.dylib                   0x00000001018c34d8 _dispatch_client_callout + 20
16  libdispatch.dylib                   0x00000001018cac20 _dispatch_lane_serial_drain + 720
17  libdispatch.dylib                   0x00000001018cb834 _dispatch_lane_invoke + 440
18  libdispatch.dylib                   0x00000001018d7270 _dispatch_workloop_worker_thread + 1344
19  libsystem_pthread.dylib             0x0000000196151718 _pthread_wqthread + 276
20  libsystem_pthread.dylib             0x00000001961579c8 start_wqthread + 8

意思是在子线程中UIView对象调用了bounds,导致视频画面迟迟渲染不出来。查找发现GPUImageView的视频渲染类的- (void)recalculateViewGeometry 方法中有两处调用了 bounds

解决办法
GPUImageView中声明一个属性viewBounds来保存viewbounds值,在- (void)layoutSubviews方法中给viewBounds赋值,用viewBounds代替bounds就可以了,此时就不卡顿了。

- (void)layoutSubviews {
    [super layoutSubviews];
    self.viewBounds = self.bounds;
    // The frame buffer needs to be trashed and re-created when the view size changes.
    if (!CGSizeEqualToSize(self.bounds.size, boundsSizeAtFrameBufferEpoch) &&
        !CGSizeEqualToSize(self.bounds.size, CGSizeZero)) {
        runSynchronouslyOnVideoProcessingQueue(^{
            [self destroyDisplayFramebuffer];
            [self createDisplayFramebuffer];
            [self recalculateViewGeometry];
        });
    }
}

- (void)recalculateViewGeometry;
{
    runSynchronouslyOnVideoProcessingQueue(^{
        CGFloat heightScaling, widthScaling;
       
        CGSize currentViewSize = self.viewBounds.size;
       
        //    CGFloat imageAspectRatio = inputImageSize.width / inputImageSize.height;
        //    CGFloat viewAspectRatio = currentViewSize.width / currentViewSize.height;
       
        CGRect insetRect = AVMakeRectWithAspectRatioInsideRect(inputImageSize, self.viewBounds);

xcode11iOS13开始,系统对在子线程中做UI操作要求更加严格。千万不要在子线程中使用与UI相关的代码(卡顿只会在调试模式有问题,上线后不会卡顿)。

上一篇下一篇

猜你喜欢

热点阅读