• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:56:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

From the docs on CIContext drawImage:inRect:fromRect: :

.. On iOS 6, this method is asynchronous ..

所以如果我在 CADisplayLink 中使用它它遇到了一个问题,因为它将继续以 60fps 的速度触发异步绘图,而实际绘图可能无法跟上。

- (void) displayLinkDidFireCADisplayLink *)displatLink;
{
    CFTimeInterval duration = [displatLink duration];
    CGFloat fps = round (1.0 / duration);
    NSLog(@"%f fps", fps); // Always logs 60 fps since drawImage is async

    // This method is fast since a CIImage is just a 'recipe' for an image
    CIImage * result = [Helper generateCIImage];

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

我该如何解决这个问题?


编辑 - 更多信息

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

self.eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

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

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

NSURL * testImageURL = [[NSBundle mainBundle] URLForResource"image" withExtension"jpg"];
NSAssert(nil != testImageURL, @"Image not found");

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

[EAGLContext setCurrentContext:self.eaglContext];

self.displayLink = [CADisplayLink displayLinkWithTarget:self selectorselector(displayLinkDidFire];

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



Best Answer-推荐答案


解决方案是使用“OpenGL ES 渲染循环”,而不是尝试使用 CADisplayLink 构建一个。幸运的是,这很容易,因为 GLKViewController自动执行此操作:

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

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

// The GLKViewController automatically calls this method
- (void) updateScreen
{        
    CIImage * result = [Helper generateCoreImage];

    // 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;
    [view display];
}

关于ios - CIContext如何使用drawImage :inRect:fromRect: with a CADisplayLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17898890/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap