【发布时间】:2011-12-20 15:10:38
【问题描述】:
我正在尝试在 iOS 中创建一个类似 Apple Photos 应用程序的照片查看器。 布局没问题,但它收到内存警告然后崩溃。为什么?即使我从应用程序文档文件夹中加载 7/8 图像也会发生这种情况。我必须用特定系统管理内存吗?我在 iOS 5 中使用 ARC。
编辑:
代码:
for (int i=0; i<[dataSource count]; i++) {
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[dataSource objectAtIndex:i] forState:UIControlStateNormal];
[[button titleLabel] setText:[NSString stringWithFormat:@"%i",i+1]];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[[button layer] setBorderWidth:1];
[[button layer] setBorderColor:[UIColor darkGrayColor].CGColor];
if (i==0) {
[button setFrame:CGRectMake(x, y, width, height)];
} else {
if (i%5==0) {
nRow++;
x=18;
[button setFrame:CGRectMake(x, (y*nRow), width, height)];
} else {
[button setFrame:CGRectMake(x+space+width, (y*nRow), width, height)];
x=button.frame.origin.x;
}
}
[[self view] addSubview:button];
}
这段代码的主要部分是前 6 行,之后是所有的 x 和 y。 dataSource 是一个声明为属性(非原子,强)的 NSArray。它包含 UIImage 对象。
【问题讨论】:
-
@user523234 简单接收内存警告然后崩溃
标签: ios memory photo automatic-ref-counting