0

我在我的图片库和缩略图库中加载了很多图片,每次我按下一个按钮(部分)时,我都会用正确的部分图片替换图片,自从我第 6 次到第 7 次这样做之后,我的应用程序崩溃了。所以我尝试使用分配工具进行构建,每次按下按钮时,我都会看到分配内存在增长!我已经看到堆栈内存分配不是使用 initWithContentOfFile 增长的。我究竟做错了什么?

for (UIView *view in [scroller subviews]) {

    [view removeFromSuperview];


}
for (UIView *view in [thumbnail subviews]) {

    [view removeFromSuperview];


}


for (int i=1; i<numeroimg+1; i++) {
    UIImageView *imagen = [[[UIImageView alloc] initWithImage:[[UIImage alloc] initWithContentsOfFile:
                                                               [[NSBundle mainBundle]
                                                                pathForResource:[NSString stringWithFormat:@"%@%d",sezione,i]
                                                                ofType:@"jpg"]]]autorelease];
    imagen.frame = CGRectMake((i-1)*1024, 0, 1024, 704);

    [scroller addSubview:imagen];

    UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake((i-1)*210, 30, 200, 150)]autorelease];
    [button setImage:[[UIImage alloc] initWithContentsOfFile:
                      [[NSBundle mainBundle]
                       pathForResource:[NSString stringWithFormat:@"%@%d",sezione,i]
                       ofType:@"jpg"]] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(pagina:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = i;
    [thumbnail addSubview:button];
}
4

2 回答 2

0

我究竟做错了什么?

+alloc用于创建图像:

UIImageView *imagen = [[[UIImageView alloc] initWithImage:...

但你永远不会释放imagen

于 2012-05-04T14:05:37.837 回答
0

尝试手动排空自动释放池

@autoreleasepool{
UIImageView *imagen = [[[UIImageView alloc] initWithImage:[[UIImage alloc] initWithContentsOfFile:
                                                               [[NSBundle mainBundle]
                                                                pathForResource:[NSString stringWithFormat:@"%@%d",sezione,i]
                                                                ofType:@"jpg"]]]autorelease];
    imagen.frame = CGRectMake((i-1)*1024, 0, 1024, 704);

    [scroller addSubview:imagen];

    UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake((i-1)*210, 30, 200, 150)]autorelease];
    [button setImage:[[UIImage alloc] initWithContentsOfFile:
                      [[NSBundle mainBundle]
                       pathForResource:[NSString stringWithFormat:@"%@%d",sezione,i]
                       ofType:@"jpg"]] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(pagina:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = i;
    [thumbnail addSubview:button];
}
于 2012-05-07T16:27:09.793 回答