UIImagaView的Xcode分配问题不断增长

时间:2012-05-04 08:03:27

标签: objective-c xcode memory-management

我在我的图片库和我的缩略图库中加载了大量图片,每次按下按钮(部分)我用右侧的seciont图像替换图像,一切都好,因为第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];
}

2 个答案:

答案 0 :(得分:0)

  

我做错了什么?

您正在使用+alloc创建图片:

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

但您永远不会发布imagen

答案 1 :(得分: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];
}