使用 MapKit 的覆盖视图获取内存警告级别 2
我遇到了有关 iPhone 内存警告的问题。我删除覆盖并插入新的。这种情况每 5 秒发生一次。但过了一会儿,比如半分钟,我收到内存警告,我的应用程序崩溃了。
我能做什么,我的覆盖层的子视图是问题所在......
三角形将使用“drawRect”创建。
我测试了一下,没有三角形就没有问题,应用程序运行稳定。
但只有当我将三角形添加到circleView时。
当三角形类为空时,也存在同样的问题。
- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView *overlayReturn = nil;
if ([overlay isKindOfClass:[MKCircle class]] == YES) {
MKCircleView *circleView = [[[MKCircleView alloc] initWithOverlay:overlay]autorelease] ;
circleView.strokeColor = [UIColor redColor];
circleView.lineWidth = 1;
circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
Triangle* triangle = [[Triangle alloc]initWithFrame:CGRectMake(circleView.circle.radius*10-1000, circleView.circle.radius*10-1000, 2000, 2000)];
triangle.backgroundColor = [UIColor clearColor];
[circleView addSubview:triangle];
[triangle release];
return circleView;
}
I have a problem concerning memory warnings on the iPhone. I remove overlays and insert new ones. This happens every 5 seconds. But after a while, like half a minute, I get a memory warning and my app crashes.
What could I do, the subview of my overlay is the problem...
The triangle will be created with "drawRect".
I tested it, without the triangle it would be no problem and the app runs stable.
But only when I add the triangle to the circleView.
When the triangle class is empty, there is the same problem.
- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView *overlayReturn = nil;
if ([overlay isKindOfClass:[MKCircle class]] == YES) {
MKCircleView *circleView = [[[MKCircleView alloc] initWithOverlay:overlay]autorelease] ;
circleView.strokeColor = [UIColor redColor];
circleView.lineWidth = 1;
circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
Triangle* triangle = [[Triangle alloc]initWithFrame:CGRectMake(circleView.circle.radius*10-1000, circleView.circle.radius*10-1000, 2000, 2000)];
triangle.backgroundColor = [UIColor clearColor];
[circleView addSubview:triangle];
[triangle release];
return circleView;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从仪器的泄漏和分配开始,看看是什么占用了您的内存。如果我不得不猜测的话,您可能会泄漏 Triangle 类中的某些内容。
Start with Instrument's Leaks and Allocations to see what's taking up your memory. You're probably leaking something inside the
Triangle
class if I had to guess.我也遇到过几乎同样的问题。看来该套件不能很好地处理多个叠加。无论它是 MKCircle、MKPolygon 还是自定义,都没关系...
问题是 viewForOverlay 不会重用您的叠加层。因此每次都会创建一个新的叠加和三角形。
这个问题的答案中描述了一种解决方法。不过,它位于 Apple 开发者论坛上:获取它 此处...
这样,您将创建一个包含所有叠加层的叠加层。导致内存使用量大大减少。
I've had almost the same problem. It just seems that the kit is not handling multiple overlays that well. It doesn't matter if it is an MKCircle, MKPolygon or custom...
The problem is that viewForOverlay isn't reusing your overlays. Therefore creating a new overlay AND triangle each time.
There is a workaround described in the answer to this question. It is on the Apple Developer forums though: get it here...
This way you'll create one overlay with all your overlays in it. Resulting in far less memory usage.
我不确定你如何使用该方法(在循环中,或者什么),但是,如果可以的话,设置另一个 NSAutoreleasepool 来尽快释放这些对象。
下面是一个示例,说明如何将它与循环一起使用以尽快删除自动释放的对象。
I'm not sure how you're using that method (in a loop, or what), but, if you can, set up another NSAutoreleasepool to release those objects as soon as you can.
Here's an example of how you could use it with a loop to get rid of autoreleased objects ASAP.