iphone - 在 setimage 上收到内存警告

标签 iphone ios xcode ipad ios5

这个问题完全难住了我。这是适用于带有 Xcode 4.2 的 iOS 5.0

发生的事情是,在我的应用程序中,我让用户从他们的相册中选择图像,并将这些图像保存到应用程序文档目录中。非常简单。

然后我所做的是,在其中一个 viewController.m 文件中,我创建了多个 UIImageView,然后我从用户从应用程序目录中选择的其中一张图片中为 ImageView 设置图像。问题是,在一定数量的 UIImage 集之后,我收到“收到内存警告”。它通常发生在有 10 张图片时。如果假设用户选择了 11 张图片,则应用程序会因错误 (GBC) 而崩溃。注意:每张图片至少 2.5 MB。

经过数小时的测试,我终于将问题缩小到这行代码

[button1AImgVw setImage:image];

如果我注释掉那段代码。所有编译都很好,没有内存错误发生。但是,如果我不注释掉该代码,我会收到内存错误并最终导致崩溃。另请注意,它确实处理了整个 CreateViews IBAction,但最后仍然崩溃。我无法执行 release 或 dealloc,因为我在带有 Xcode 4.2 的 iOS 5.0 上运行它

这是我使用的代码。谁能告诉我我做错了什么?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self CreateViews];
}

-(IBAction) CreateViews
{
    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
    documentsPath = [paths objectAtIndex:0]; 

    //here 15 is for testing purposes    
    for (int i = 0; i < 15; i++) 
    {    
        //Lets not get bogged down here. The problem is not here
        UIImageView *button1AImgVw = [[UIImageView alloc] initWithFrame:CGRectMake(10*i, 10, 10, 10)];
        [self.view addSubview:button1AImgVw];

        NSMutableString *picStr1a = [[NSMutableString alloc] init];
        NSString *dataFile1a = [[NSString alloc] init];

        picStr1a = [NSMutableString stringWithFormat:@"%d.jpg", i];
        dataFile1a = [documentsPath stringByAppendingPathComponent:picStr1a];
        NSData *potraitImgData1a =[[NSData alloc] initWithContentsOfFile:dataFile1a];
        UIImage *image = [[UIImage alloc] initWithData:potraitImgData1a];

        // This is causing my app to crash if I load more than 10 images!
    //  [button1AImgVw setImage:image];

//If I change this code to a static image. That works too without any memory problem.
button1AImgVw.image = [UIImage imageNamed:@"mark-yes.png"]; // this image is less than 100KB
        }

        NSLog(@"It went to END!");

    }

这是我在选择 10 张图像时遇到的错误。应用程序确实启动并运行

2012-10-07 17:12:51.483 ABC-APP[7548:707] It went to END!
2012-10-07 17:12:51.483 ABC-APP [7531:707] Received memory warning.

当有 11 张图片时,应用程序崩溃并出现此错误

2012-10-07 17:30:26.339 ABC-APP[7548:707] It went to END!
(gbc)

最佳答案

在我的 iOS 编程生涯中,这种情况(内存警告和应用程序在尝试将多个全分辨率 UIImages 加载到 View 中时退出)曾多次让我心烦意乱。

在执行“setImage”调用之前,您需要制作原始图像的缩小副本。

对于我自己的代码,我使用“UIImage+Resize”类别,the details for which can be found here .

在插入您的 View 之前将您的图像调整到更小的尺寸,然后确保发布全分辨率图像(如果在 ARC 上则设置为 nil),您应该会有更快乐的时光。

这是我在自己的代码中的做法:

CGSize buttonSize = CGSizeMake(width, height);
// it'd be nice if UIImage took a file URL, huh?
UIImage * newImage = [[UIImage alloc] initWithContentsOfFile: pathToImage];
if(newImage)
{
    // this "resizedimage" image is what you want to pass to setImage
    UIImage * resizedImage = [newImage resizedImage: buttonSize interpolationQuality: kCGInterpolationLow];
}

关于iphone - 在 setimage 上收到内存警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12773074/

相关文章:

iPhone 向右翻转按钮(如 iTunes)

iphone - UITableView 上显示的多个附件复选标记

swift - 在 macOS 中获取用户偏好的温度设置

ios - 尝试解析 iOS 项目的 restful web APS 返回的 json 数组

iphone - NSDateFormatter 时区(Etc/GMT)未解析 dateFromString

iphone - Adwhirl 在 UIViewController 类中不起作用

ios - 使用 UITextViews 作为 UITextFields

jquery - iOS HTML Web App (PhoneGap) 过度滚动

iphone - 我什么时候应该关闭计时器?

ios - 如何复制 Xcode 项目以创建免费应用程序的付费版本