首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImagaView的Xcode分配问题不断增加

UIImagaView的Xcode分配问题不断增加
EN

Stack Overflow用户
提问于 2012-05-04 16:03:27
回答 2查看 296关注 0票数 0

我在我的图片库和缩略图库中加载了很多图片,每次我按下按钮(部分)时,我都会用正确的seciont图片替换图片,自从第6-7次我这样做我的应用程序崩溃以来,一切都很好。因此,我尝试使用分配工具进行构建,每次我按下按钮时,我都会看到分配内存在增长!我一直希望看到的是堆栈内存分配,而不是initWithContentOfFile的增长。我做错了什么?

代码语言:javascript
复制
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];
}
EN

回答 2

Stack Overflow用户

发布于 2012-05-04 22:05:38

我做错了什么?

您正在使用+alloc创建一个镜像:

代码语言:javascript
复制
UIImageView *imagen = [[[UIImageView alloc] initWithImage:...

但你永远不能释放imagen

票数 0
EN

Stack Overflow用户

发布于 2012-05-08 00:27:10

尝试手动排出自动释放池

代码语言:javascript
复制
@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];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10444987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档