IOS:使用模式图像作为背景 - 内存泄漏 [英] IOS: Using A Pattern Image As A Background -Memory Leak

查看:115
本文介绍了IOS:使用模式图像作为背景 - 内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

行。我会寻找答案,也可以自己找。我有一个讨厌回答自己问题的习惯。

OK. I'll be looking for the answer, and may find it myself. I have a nasty habit of answering my own questions.

无论如何,我有一个应用程序设计为相当容易皮肤化。作为其中的一部分,我在静态类中隔离了特定于变体的方法。这些静态方法提供特定于变体的主应用程序图像,颜色和设置。 .h文件对于主应用程序是通用的,但.m文件特定于变体。

In any case, I have an app that is designed to be "skinned" fairly easily. As part of that, I have sequestered methods in a static class that is specific to variants. These static methods feed the main app images, colors and settings specific to the variant. The .h file is common to the main app, but the .m file is specific to the variant.

我喜欢使用该功能将图像作为背景发送(这是自动平铺的),因此接口文件指定例程返回UIColor,如下所示:

I like to use the ability to send an image as a background (which is automagically tiled), so the interface file specifies the routine as returning a UIColor, like so:

+ (UIColor *)meetingDetailBackgroundColor;

但是实现文件会加载一个图像文件,并返回它,如下所示:

But the implementation file loads an image file, and returns that, like so:

+ (UIColor *)meetingDetailBackgroundColor
{
    return [UIColor colorWithPatternImage:[UIImage imageNamed:@"DarkWeave.png"]];
}

在上下文中使用,如下所示:

Which is used in context, like so:

[[self view] setBackgroundColor:[BMLTVariantDefs meetingDetailBackgroundColor]];

注意:编辑恢复我使用的原始简单代码。

NOTE: Edited to restore the original simple code I used.

现在,问题在于我有时(并非总是)会泄漏。

Now, the issue is that I sometimes (not always) get a leak.

我确定我在这里做些什么事。我只是不确定是什么。

I'm sure that I'm doing something hinky here. I'm just not sure what.

任何想法?

BTW:这是一个ARC程序,正在运行在IOS 5.我是ARC新手,但我认为这是我应该这样做的方式。

BTW: This is an ARC program, running on IOS 5. I'm new to ARC, but I think this is the way I'm supposed to do it.

推荐答案

UIColor colorWithPatternImage 有错误,请勿使用它。我的经验是,它往往会严重削弱设备的性能,但不会影响模拟器。像滚动或动画一样的东西往往会变慢。我不确定这是否真的有资格作为泄漏,我没有看到App因为RAM耗尽而被杀死。但是如果你对应用程序进行了分析,你会看到应用程序运行得慢得多,启用了 UIColor colorWithPatternImage 并绘制了一些东西。

UIColor colorWithPatternImage is buggy, do not use it. My experience is that it tends to greatly cripple performance on the device but not in the simulator. Anything like scrolling or animation on top of it tends to get slow. I'm not sure whether this really qualifies as a leak, I'm not seeing App being killed because RAM ran out. But if you profile the app, you will see that the app runs much slower with UIColor colorWithPatternImage enabled and drawing something.

最终我创建了一个 UIView 的子类,并做了类似这样的事情:

Eventually I created a subclass of UIView, and did something like this:

- (void)drawRect:(CGRect)rect
{
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(c, kCGBlendModeCopy);
    CGContextDrawTiledImage(c, CGRectMake(0, 0, bkgnd.size.width, bkgnd.size.height), bkgnd.CGImage);
}

这将平铺图像。然后我使用 self.tableView.backgroundView [self.view insertSubview:bkgnd atIndex:0] 来制作它背景。它在设备上运行得更快,并且导致零内存泄漏。

This will tile the image. I then either use self.tableView.backgroundView or [self.view insertSubview:bkgnd atIndex:0] to make it a background. It runs much faster on the device, and causes zero memory leaks.

这篇关于IOS:使用模式图像作为背景 - 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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