如何将核心图形操作优化为该方法?

发布于 2024-12-01 01:37:11 字数 892 浏览 1 评论 0原文

我已经分析了我的应用程序,它大部分时间都花在我编写的将两个 UIImage 组合在一起的类方法中:

+ (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {  

    // Check if UIGraphicsBeginImageContextWithOptions is supported as this can create alphaless contexts and is faster
    if (UIGraphicsBeginImageContextWithOptions != NULL) {
        UIGraphicsBeginImageContextWithOptions(image1.size, YES, 0.0);
    }
    else{

        UIGraphicsBeginImageContext(image1.size);  
    }
    // Draw image1  
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];  

    // Draw image2  
    [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];  

    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();  

    UIGraphicsEndImageContext();  

    return resultingImage;  
}

有任何专家可以建议优化此方法的方法吗?或者是在另一个线程中运行它和/或尝试不首先调用它,除非绝对必要?

I have profiled my app and it spends most of its time in this class method I wrote to combine two UIImages together:

+ (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {  

    // Check if UIGraphicsBeginImageContextWithOptions is supported as this can create alphaless contexts and is faster
    if (UIGraphicsBeginImageContextWithOptions != NULL) {
        UIGraphicsBeginImageContextWithOptions(image1.size, YES, 0.0);
    }
    else{

        UIGraphicsBeginImageContext(image1.size);  
    }
    // Draw image1  
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];  

    // Draw image2  
    [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];  

    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();  

    UIGraphicsEndImageContext();  

    return resultingImage;  
}

Any gurus out there that can suggest ways of optimising this method? Or is it a case of running it in another thread and/or trying not to call it in the first place unless it is absolutely necessary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何时共饮酒 2024-12-08 01:37:11

当你说“大部分时间”时,这当然是不明确的,因为它可能包括或不包括被调用者,
它可以是 CPU 时间或挂钟时间。

无论如何,您都需要行级信息。
我所做的只是暂停几次并检查调用堆栈。
占用大量时间的语句优先出现在堆栈上。
如果我想让它运行得更快,我需要找到这样一行我可以避免执行的代码。

When you say "most of its time", that's ambiguous of course, because it could include callees or not,
and it could be CPU time or wall-clock time.

In any case, you want line-level information.
What I do is just pause it a few times and examine the call stack.
Statements that are responsible for significant time appear preferentially on the stack.
If I want it to go faster, I need to find such a line of code that I can avoid executing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文