drawViewHierarchyInRect 与 renderInContext 的预期性能是什么 [英] What is expected performance of drawViewHierarchyInRect vs renderInContext

查看:115
本文介绍了drawViewHierarchyInRect 与 renderInContext 的预期性能是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览完所有相关问题后,我明白 drawViewHierarchyInRect 应该比 renderInContext 快得多,但这不是我所看到的.

这是我的带时间分析的快照功能:

- (UIImage *)screenshot{UIScreen *mainScreen = [UIScreen mainScreen];CGSize imageSize = mainScreen.bounds.size;如果 (&UIGraphicsBeginImageContextWithOptions != NULL) {UIGraphicsBeginImageContextWithOptions(imageSize, NO, 1);} 别的 {UIGraphicsBeginImageContext(imageSize);}CGContextRef 上下文 = UIGraphicsGetCurrentContext();NSArray *windows = [[UIApplication sharedApplication] windows];for (UIWindow *window in windows) {if (![window RespondsToSelector:@selector(screen)] || window.screen == mainScreen) {NSDate *methodStart = [NSDate 日期];//[window.layer renderInContext:context];[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO];NSDate *methodFinish = [NSDate 日期];NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];NSLog(@"executionTime = %f", executionTime);}}UIImage *image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();返回图像;}

测试是在装有 IOS 9.0 的 iPad Air 上完成的作为测试应用程序,我使用 UIKitCatalog 应用程序 https://developer.apple.com/library/ios/samplecode/UICatalog/Introduction/Intro.html

该应用显示 2 个窗口:

使用 drawViewHierarchyInRect 在两个窗口中的任何上产生 0.078srenderInContext 在主窗口上产生 0.045s,在 UITextEffectsWindow 上产生一些更小的时间 (0.000028s).

  1. 为什么 drawViewHierarchyInRect 比 renderInContext 慢,难道它不应该更快

  2. 对于以任何合适的帧速率录制屏幕视频,两者(78 毫秒、45 毫秒)都是不可接受的.好像出了点问题,应该快多了.

  3. [window.layer renderInContext...] 导致屏幕截图混乱且不准确,我尝试过 [window.layer.presentationLayer renderInContext...],但这会导致应用程序崩溃,我不能找不到原因.

我很感激这方面的任何帮助,我所做的就是以可能的最高帧速率录制屏幕视频,到目前为止我得到的结果没有意义,或者我可能做错了什么.

谢谢

解决方案

你问:

<块引用>

  1. 为什么 drawViewHierarchyInRectrenderInContext 慢,不是应该更快吗?

这完全取决于您要捕获的内容.例如,用一堆简单的 UIView 子视图捕获视图,我看不出有什么明显区别.当使用复杂的 UIImageView(例如非常高分辨率的图像)捕获视图时,我发现 drawViewHierarchyInRect 在极端情况下的速度大约可以提高 10 倍.我发现后者的性能差异与 Apple 在 在 iOS 中实现引人入胜的 UI 他们的例子包括一堆图像视图.

我从这些诊断中推断 drawViewHierarchyInRect 已针对带有 UIImageView 对象的屏幕快照进行了优化.因此,虽然它可能针对特定场景进行了高度优化,但我并不完全感到惊讶,因为可能存在不打算用于的场景,不会产生相同的结果.

<块引用>

  1. 对于以任何合适的帧速率录制屏幕视频,两者(78 毫秒、45 毫秒)都是不可接受的.好像出了点问题,应该会快很多.

这些不是为视频拍摄而设计的,所以我不确定您所说的有些地方非常错误"是什么意思.为什么说应该快很多"?

<块引用>

  1. [window.layer renderInContext...] 导致屏幕截图混乱且不准确...

您应该编辑您的问题以包含屏幕快照以向我们展示您的意思.

<块引用>

...我已经尝试过 [window.layer.presentationLayer renderInContext...],但这导致应用程序崩溃,我找不到原因.

presentationLayer 方法用于获取当前呈现状态的副本(通常用于查询中间动画),但它不是您通常与之交互的层.

After browsing through all the relevant question on SO, I understand drawViewHierarchyInRect is supposed to be much faster than renderInContext, yet this is not what I am seeing.

Here is my snapshot function with time profiling:

- (UIImage *)screenshot
{
    UIScreen *mainScreen = [UIScreen mainScreen];
    CGSize imageSize = mainScreen.bounds.size;
    if (&UIGraphicsBeginImageContextWithOptions != NULL) {
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 1);
    } else {
        UIGraphicsBeginImageContext(imageSize);
    }

    CGContextRef context = UIGraphicsGetCurrentContext();

    NSArray *windows = [[UIApplication sharedApplication] windows];
    for (UIWindow *window in windows) {
        if (![window respondsToSelector:@selector(screen)] || window.screen == mainScreen) {
            NSDate *methodStart = [NSDate date];

//            [window.layer renderInContext:context];
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO];

            NSDate *methodFinish = [NSDate date];
            NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
            NSLog(@"executionTime = %f", executionTime);
        }
    }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

Tests were done on iPad Air with IOS 9.0 As a test app, I am using UIKitCatalog application https://developer.apple.com/library/ios/samplecode/UICatalog/Introduction/Intro.html

The app shows 2 windows:

<UIWindow: 0x134e4d8f0; frame = (0 0; 768 1024); gestureRecognizers = <NSArray: 0x134e27740>; layer = <UIWindowLayer: 0x134e4d130>>,
<UITextEffectsWindow: 0x134e6a3a0; frame = (0 0; 768 1024); opaque = NO; autoresize = W+H; layer = <UIWindowLayer: 0x134e6aba0>>

Using drawViewHierarchyInRect results in 0.078s on any of the two windows renderInContext results in 0.045s on the main window and some smaller (0.000028s) time on the UITextEffectsWindow.

  1. Why is drawViewHierarchyInRect slower than renderInContext, isn't it supposed to be faster

  2. Both (78ms, 45ms) are unacceptable for recording video of the screen with any decent frame rate. Seems like something is very wrong, it should be much faster.

  3. [window.layer renderInContext...] results in screenshots which are messy and inaccurate, I've tried [window.layer.presentationLayer renderInContext...], yet this results in application crashes and I can't find the reason.

I would appreciate any help with this, all I am trying to do is record a video of the screen at the highest frame rate possible and so far the results I am getting do not make sense or I might be doing something very wrong.

Thanks

解决方案

You asked:

  1. Why is drawViewHierarchyInRect slower than renderInContext, isn't it supposed to be faster?

It depends entirely upon what you're capturing. For example, capturing a view with a bunch of simple UIView subviews, I see no appreciable difference. When when capturing a view with complex UIImageView (e.g. a very high resolution image), I find that drawViewHierarchyInRect can be roughly 10 times as fast in extreme situations. I find that latter performance difference comparable to what Apple claimed in Implementing Engaging UI in iOS where their example included a bunch of image views.

I infer from those diagnostics that drawViewHierarchyInRect is optimized for screen snapshots with UIImageView objects. So while it might be highly optimized for a particular scenario, I'm not entirely surprised that there might exist scenarios, for which it wasn't intended, that don't yield the same results.

  1. Both (78ms, 45ms) are unacceptable for recording video of the screen with any decent frame rate. Seems like something is very wrong, it should be much faster.

These weren't designed for video capture, so I'm not sure what you mean by "something is very wrong." Why do you say "it should be much faster"?

  1. [window.layer renderInContext...] results in screenshots which are messy and inaccurate ...

You should edit your question to include screen snapshot to show us what you mean.

... I've tried [window.layer.presentationLayer renderInContext...], yet this results in application crashes and I can't find the reason.

The presentationLayer method is for getting a copy of the current presentation state (generally used for inquiring mid-animation), but it's not a layer with which you generally interact.

这篇关于drawViewHierarchyInRect 与 renderInContext 的预期性能是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆