xcode 太多打开的文件 iOS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16338608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:18:44  来源:igfitidea点击:

Too many open files iOS

iosxcodeuiimageviewuiimage

提问by Le'Kirdok

i am new on ios programming, when my app is running , i am taking these errors. I am loading 950+ images in my app and i am using ARC.

我是 ios 编程的新手,当我的应用程序运行时,我正在接受这些错误。我正在我的应用程序中加载 950 多个图像,并且我正在使用 ARC。

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/menu-24-20.png'error = 24 (Too many open files)

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/menu-24-20.png'error = 24 (Too many open files)

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/circle_green.png' error = 24 (Too many open files)

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/circle_green.png' error = 24 (Too many open files)

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/shopping_cart_1-512.png' error = 24 (Too many open files)

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/shopping_cart_1-512.png' error = 24 (Too many open files)

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/menu-24-20.png'error = 24 (打开的文件太多)

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/menu-24-20.png'error = 24 (打开的文件太多)

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/circle_green.png' error = 24 (Too many open文件)

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/circle_green.png' error = 24 (Too many open文件)

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/shopping_cart_1-512.png'错误=许多打开的文件)

ImageIO: CGImageRead_mapData 'open' failed '/Users/apple/Library/Application Support/iPhone Simulator/6.1/Applications/16551664-4694-4742-85DC-2C3C0ADC5289/demo.app/shopping_cart_1-512.png'错误=许多打开的文件)

This code block is part of my app.

此代码块是我的应用程序的一部分。

                while(sqlite3_step(compiledStatement) == SQLITE_ROW)  {
                    int UrunId = sqlite3_column_int(compiledStatement,0);
                    //NSString *urunNo= [NSString stringWithFormat:@"%i",UrunId];
                    UrunAdi = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];
                    NSString *imageName= [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 2)];
                    UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+30, row*350, 175, 280)];

                    NSString *filePathBackGround = [[NSBundle mainBundle] pathForResource:@"BackImage" ofType:@"png"];
                    NSData *myData = [NSData dataWithContentsOfFile:filePathBackGround];                        
                    UIImage *blackBackGround = [UIImage imageWithData:myData];

                    [background setImage:blackBackGround];                                            
                    [scrollView addSubview:background];



                    NSString *filePathSepeteEkleButton = [[NSBundle mainBundle] pathForResource:@"sepeteEkleButtonImage" ofType:@"png"];
                    NSData *myDataButton = [NSData dataWithContentsOfFile:filePathSepeteEkleButton];

                    UIImage *sepeteEkleButtonImage = [UIImage imageWithData:myDataButton];
                    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
                    [button setFrame:CGRectMake(column*197+38 , row*350+8, 159, 45)];
                    [button setImage:sepeteEkleButtonImage forState:UIControlStateNormal];
                    [button addTarget:self
                               action:@selector(addToChart:)
                     forControlEvents:UIControlEventTouchUpInside];
                    button.tag = UrunId;



                    UILabel *buttonLabel=[[UILabel alloc]initWithFrame:CGRectMake(column*197+43,row*350+20,200,20)];
                    buttonLabel.textColor = [UIColor whiteColor];
                    buttonLabel.backgroundColor=[UIColor clearColor];
                    buttonLabel.text=UrunAdi;

                    [scrollView addSubview:button];

                    [scrollView addSubview:buttonLabel];


                    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
                    NSString *filePath = [documentsPath stringByAppendingPathComponent:imageName];

                    NSData *pngData = [NSData dataWithContentsOfFile:filePath];
                    if (pngData == nil) {
                        NSString *filePathResimYok= [[NSBundle mainBundle] pathForResource:@"resimYok" ofType:@"jpeg"];
                        NSData *myDataResimYok= [NSData dataWithContentsOfFile:filePathResimYok];
                        image2 = [UIImage imageWithData:myDataResimYok];//horoda
                    }else{
                        image2 = [UIImage imageWithData:pngData];
                    }
                    image2=[image2 imageByScalingAndCroppingForSize:CGSizeMake(175, 210)];

                    //UIImage *urunDetayImage = [UIImage imageNamed:image2];
                    UIButton * urunDetayButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    [urunDetayButton setFrame:CGRectMake(column*197+38 , row*350+58, 159, 170)];
                    [urunDetayButton setImage:image2 forState:UIControlStateNormal];
                    [urunDetayButton addTarget:self
                                        action:@selector(buttonClicked:)
                              forControlEvents:UIControlEventTouchUpInside];
                    urunDetayButton.tag = UrunId;
                    [scrollView addSubview:urunDetayButton];

                    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(column*197+38,row*350+245,200,20)];
                    label.textColor = [UIColor whiteColor];
                    label.backgroundColor=[UIColor clearColor];
                    label.text=UrunAdi;
                    [scrollView addSubview:label];

I am trying to fix for 3 days.Please help me. Thanks.

我正在尝试修复 3 天。请帮助我。谢谢。

回答by David H

As far as I can see your code looks fine, and running in the simulator (where plenty of memory) this should be working. A few suggestions:

据我所知,您的代码看起来不错,并且在模拟器(内存充足的地方)中运行应该可以正常工作。几点建议:

1) use dataWithContentsOfFile:options:error:instead of dataWithContentsOfFile:, use the NSDataReadingUncached option (to reduce memory pressure on the system), and test the return value - if nil log the error and update your question.

1) 使用dataWithContentsOfFile:options:error:代替dataWithContentsOfFile:,使用 NSDataReadingUncached 选项(以减少系统内存压力),并测试返回值 - 如果 nil 记录错误并更新您的问题。

2) You can always dump the data image in a NSCache object, and if the system needs memory it will release cache items, and you'll have to re-read the image from the file system. You can use this technique even if you only pull a few images at a time.

2) 你总是可以将数据图像转储到一个 NSCache 对象中,如果系统需要内存它会释放缓存项,你必须从文件系统中重新读取图像。即使您一次只提取几个图像,您也可以使用此技术。

3) You can use UIImage imageWithContentsOfFile:instead of getting the data then creating the image. In this case put the UIImage itself in the cache, with a key of its name.

3)您可以使用 UIImageimageWithContentsOfFile:而不是获取数据然后创建图像。在这种情况下,将 UIImage 本身放入缓存中,并使用其名称作为键。

回答by Saran Jasty

From X-Code 10 Apple is not removing all NSCache instances dynamically as it done before. So as developer we need to Quit the simulator app and run the build your build be succeeded.

从 X-Code 10 开始,Apple 并没有像以前那样动态删除所有 NSCache 实例。因此,作为开发人员,我们需要退出模拟器应用程序并运行构建您的构建才能成功。