1

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.

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)

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.

Kirdok
  • 1,904
  • 2
  • 22
  • 27
  • Include only relevant code. – Marcus Adams May 02 '13 at 13:03
  • 2
    I think the problem is with `loading 950 images into my app'. Unless they are tiny, you are only ever going to be able to display a small fraction of these, whilst the remainder are occupying scarce memory. – marko May 02 '13 at 13:18
  • 2
    Not sure why you are loading that many images at one time. You should only ever need to load the images that are displayed and the rest can wait until they are going to be displayed before you load them. If you use a `UICollectionView` then this capability already exists for you. – iwasrobbed May 02 '13 at 13:22

2 Answers2

4

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.

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.

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.

David H
  • 40,852
  • 12
  • 92
  • 138
  • Thank you, Now i can see the images, but now i am taking this error *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name '2-view-5' and directory 'MainStoryboard_iPad.storyboardc'' but i guess this one is not related with images error right? – Kirdok May 02 '13 at 14:48
  • Comment out the image fetches and see if you still get the second error. I don't know storyboards so if you still get the error post a new question. Good luck! – David H May 02 '13 at 14:51
  • i am not using the dataWithContentsOfFile or any other. what can i do for solve this issue ? – ios developer Jul 20 '16 at 07:54
0

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.