菜鸟教程小白 发表于 2022-12-12 12:56:33

ios - CIContext如何使用drawImage :inRect:fromRect: with a CADisplayLink


                                            <p><p> <a href="http://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/QuartzCoreFramework/Classes/CIContext_Class/Reference/Reference.html#//apple_ref/occ/instm/CIContext/drawImage%3ainRect%3afromRect%3a" rel="noreferrer noopener nofollow">From the docs on <code>CIContext drawImage:inRect:fromRect:</code></a> :</p>

<blockquote>
<p>.. On iOS 6, this method is asynchronous ..</p>
</blockquote>

<p>所以如果我在 <a href="http://developer.apple.com/library/ios/#documentation/QuartzCore/Reference/CADisplayLink_ClassRef/Reference/Reference.html" rel="noreferrer noopener nofollow">CADisplayLink</a> 中使用它它遇到了一个问题,因为它将继续以 60fps 的速度触发异步绘图,而实际绘图可能无法跟上。 </p>

<pre><code>- (void) displayLinkDidFire:(CADisplayLink *)displatLink;
{
    CFTimeInterval duration = ;
    CGFloat fps = round (1.0 / duration);
    NSLog(@&#34;%f fps&#34;, fps); // Always logs 60 fps since drawImage is async

    // This method is fast since a CIImage is just a &#39;recipe&#39; for an image
    CIImage * result = ;

    // This drawing is unable to keep up with the calls to the displayLinkDidFire method
    [self.ciContext drawImage:result
                     inRect:self.destFrame
                     fromRect:self.targetFrame];
}
</code></pre>

<p>我该如何解决这个问题?</p>

<hr/>

<p><strong>编辑 - 更多信息</strong></p>

<p>我正在使用带有 <code>EAGLContext</code> 的 CoreImage(根据 WWDC 以获得更好的绘图性能)。</p>

<pre><code>self.eaglContext = [ initWithAPI:kEAGLRenderingAPIOpenGLES2];

self.ciContext = [CIContext
                  contextWithEAGLContext:self.eaglContext
                  options: @{kCIContextWorkingColorSpace:} ];

GLKView *view = (GLKView *)self.view;
view.context = self.eaglContext;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

NSURL * testImageURL = [ URLForResource:@&#34;image&#34; withExtension:@&#34;jpg&#34;];
NSAssert(nil != testImageURL, @&#34;Image not found&#34;);

self.image = [CIImage imageWithContentsOfURL:testImageURL
                                     options:@{ kCIImageColorSpace: }];

;

self.displayLink = ;

glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>解决方案是使用“OpenGL ES 渲染循环”,而不是尝试使用 <code>CADisplayLink</code> 构建一个。幸运的是,这很容易,因为 <a href="http://developer.apple.com/library/ios/#documentation/GLkit/Reference/GLKViewController_ClassRef/Reference/Reference.html" rel="noreferrer noopener nofollow"><code>GLKViewController</code></a>自动执行此操作:</p>

<blockquote>
<p>The GLKViewController class provides all of the standard view controller functionality, but additionally implements an OpenGL ES rendering loop.</p>
</blockquote>

<p>唯一的缺点是这将您与使用 <code>GLKViewController</code> 紧密联系在一起,而不是仅仅将 <code>GLKView</code> 添加到现有的 UIView。要解决这个问题,您需要弄清楚如何实现自己的 OpenGL ES 渲染循环。 </p>

<pre><code>// The GLKViewController automatically calls this method
- (void) updateScreen
{      
    CIImage * result = ;

    // Clears the screen to a grey color
    glClearColor(0.5, 0.5, 0.5, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

    [self.ciContext drawImage:result
                     inRect:self.destFrame
                     fromRect:self.targetFrame];

    // `display` needs to be called here according to the docs.
    GLKView *view = (GLKView *)self.view;
    ;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - CIContext如何使用drawImage :inRect:fromRect: with a CADisplayLink,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/17898890/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/17898890/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - CIContext如何使用drawImage :inRect:fromRect: with a CADisplayLink