美文网首页
GPUImage -[UIView bounds]

GPUImage -[UIView bounds]

作者: Cwwng | 来源:发表于2021-07-13 16:38 被阅读0次

    一、问题

    GPUImage第一次渲染的时候卡顿。
    日志:Main Thread Checker: UI API called on a background thread: -[UIView bounds]
    原因:在子线程访问UI的属性。
    二、解决
    返回主线程。

    - (void)recalculateViewGeometry;
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            runSynchronouslyOnVideoProcessingQueue(^{
                CGFloat heightScaling, widthScaling;
                
                CGSize currentViewSize = self.bounds.size;
                
                //    CGFloat imageAspectRatio = inputImageSize.width / inputImageSize.height;
                //    CGFloat viewAspectRatio = currentViewSize.width / currentViewSize.height;
                
                CGRect insetRect = AVMakeRectWithAspectRatioInsideRect(inputImageSize, self.bounds);
                
                switch(_fillMode)
                {
                    case kGPUImageFillModeStretch:
                    {
                        widthScaling = 1.0;
                        heightScaling = 1.0;
                    }; break;
                    case kGPUImageFillModePreserveAspectRatio:
                    {
                        widthScaling = insetRect.size.width / currentViewSize.width;
                        heightScaling = insetRect.size.height / currentViewSize.height;
                    }; break;
                    case kGPUImageFillModePreserveAspectRatioAndFill:
                    {
                        //            CGFloat widthHolder = insetRect.size.width / currentViewSize.width;
                        widthScaling = currentViewSize.height / insetRect.size.height;
                        heightScaling = currentViewSize.width / insetRect.size.width;
                    }; break;
                }
                
                imageVertices[0] = -widthScaling;
                imageVertices[1] = -heightScaling;
                imageVertices[2] = widthScaling;
                imageVertices[3] = -heightScaling;
                imageVertices[4] = -widthScaling;
                imageVertices[5] = heightScaling;
                imageVertices[6] = widthScaling;
                imageVertices[7] = heightScaling;
            });
        });
        
    //    static const GLfloat imageVertices[] = {
    //        -1.0f, -1.0f,
    //        1.0f, -1.0f,
    //        -1.0f,  1.0f,
    //        1.0f,  1.0f,
    //    };
    }
    

    Xcode11和iOS13之后,系统严格要求在子线程中做UI操作。
    说明:卡顿只会在调试模式有问题,上线后不会卡顿。

    相关文章

      网友评论

          本文标题:GPUImage -[UIView bounds]

          本文链接:https://www.haomeiwen.com/subject/skjspltx.html