1 memory-leaks memory-management objective-c
我正在努力教自己Objective-c.我有很多书,我读过这些书都试图解决我的问题.我正在使用以下代码绘制自己的地图.
-(void)drawMap
{
for (NSInteger tempY=character.locationY-5;tempY<character.locationY+6;tempY++)
{
for (NSInteger tempX=character.locationX-4;tempX<character.locationX+5;tempX++)
{
MapTile *a = [[MapTile alloc] initWithFrame:CGRectMake
(((tempX-(character.locationX-3)) * 44)+6,
((tempY-(character.locationY-5)) * 44)-12,0,0)
:[map getTilePic:tempY:tempX]];
[self.view addSubview:a];
}
}
CharacterTile *a = [[CharacterTile alloc] initWithFrame:CGRectMake(138,206,0,0)];
[self.view addSubview:a];
}
Run Code Online (Sandbox Code Playgroud)
我的MapTile看起来像这样 -
-(id)initWithFrame:(CGRect)frame :(NSInteger)pictureNumber
{
if (!dungeonLoaded)
{
UIImage *tempImage;
for (NSInteger count=0;count<54;count++)
{
NSString *temp = [NSString alloc];
temp = [NSString stringWithFormat:@"%i.png", count];
tempImage = [UIImage imageNamed:temp];
loadedImage[count] = tempImage;
}
dungeonLoaded = YES;
}
CGRect rect = CGRectMake(frame.origin.x,frame.origin.y,
loadedImage[pictureNumber].size.width,
loadedImage[pictureNumber].size.height);
self = [super initWithFrame:rect];
image = [loadedImage[pictureNumber] retain];
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
return self;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果我在地图上移动一段时间,只有这是唯一被调用的方法,它只会在十几个左右移动后显着减慢.
我原本每次移动时都会加载maptile图像,并认为这可能是问题所在,所以如你所见,改变了它,只加载一次图片并重新使用它们.我尝试使用"开始Iphone游戏开发"一书中的图像缓存,并在每个drawmap方法的开头清空缓存.我尝试使用自动释放池并使用autorelease标签加载tile.我尝试在绘制瓷砖时释放瓷砖,但正如您所料,这会破坏屏幕上的图像,因此地图不可见.
我并不懒惰,并且已经尝试过广泛地让它停止放慢速度,但不幸的是,此时它超出了我的编码技能.
任何帮助将不胜感激.
提前致谢.
亚当
有很多问题:
你alloc一些MapTile在每次绘制地图,但你永远不释放他们的时刻.
你alloc一个CharacterTile在每次绘制地图,你永远不放开的时间实例.
你的MapTile构造函数分配一个NSString是从未发布实例.
你的MapTile构造函数调用retain上loadedImage[pictureNumber],但没有匹配release.
您可以重复向视图添加MapTile和CharacterTile实例,但您的代码不会从视图中删除以前的实例.
所以1-4是不好的,但我敢打赌它是#5,这会扼杀你的表现.每次更新地图时,您都会累积越来越多的子视图,并且永远不会从视图中删除旧的切片.你的代码在旧的顶部堆积新的瓷砖,堆越大,你的应用程序运行得越慢.
在任何情况下,这应该解决问题1-3:
-(void)drawMap
{
for (NSInteger tempY=character.locationY-5;tempY<character.locationY+6;tempY++)
{
for (NSInteger tempX=character.locationX-4;tempX<character.locationX+5;tempX++)
{
MapTile *a = [[MapTile alloc] initWithFrame:CGRectMake
(((tempX-(character.locationX-3)) * 44)+6,
((tempY-(character.locationY-5)) * 44)-12,0,0)
:[map getTilePic:tempY:tempX]];
[self.view addSubview:a];
[a release];
}
}
CharacterTile *a = [[CharacterTile alloc] initWithFrame:CGRectMake(138,206,0,0)];
[self.view addSubview:a];
[a release];
}
-(id)initWithFrame:(CGRect)frame :(NSInteger)pictureNumber
{
if (!dungeonLoaded)
{
UIImage *tempImage;
for (NSInteger count=0;count<54;count++)
{
NSString* temp = [NSString stringWithFormat:@"%i.png", count];
tempImage = [UIImage imageNamed:temp];
loadedImage[count] = tempImage;
}
dungeonLoaded = YES;
}
CGRect rect = CGRectMake(frame.origin.x,frame.origin.y,
loadedImage[pictureNumber].size.width,
loadedImage[pictureNumber].size.height);
self = [super initWithFrame:rect];
image = [loadedImage[pictureNumber] retain]; //FIXME: you need to release this somewhere
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
return self;
}
Run Code Online (Sandbox Code Playgroud)
问题4-5你必须自己解决.但是,如果您只是更新drawMap以便在添加任何新视图之前删除所有子视图,我认为您将大部分都在那里.
| 归档时间: |
|
| 查看次数: |
120 次 |
| 最近记录: |