Too many open files iOS - ios

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.

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.

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.

Related

Why is NSFileManager unable to find these png files?

I have written code to open image files after studying answers to the questions found here (a, b, c, d, e, f & g). But NSFileManager is unable to find them even though I added the png files to the project. I'm reasonably confident my code should be able to recognise either of the png files if they were in the right directory.
e.g.
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSLog(#"\ndirPaths %# \n\ndocsDir \n%#", dirPaths, docsDir);
NSString *imageFilePath = [docsDir stringByAppendingPathComponent:#"iTunesArtwork1024.png"];
NSLog(#"\n\nfile path should be \n\n%# \n\n", imageFilePath);
NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath];
if ([fileManager fileExistsAtPath:imageFilePath])
{
NSLog(#"\nFound file path : %#", imageFilePath);
}
else
{
NSLog(#"\nImage file not found");
}
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = image;
[self.view addSubview:logo];
}
But here is the log in the debug window
2017-07-14 18:26:35.679 IconShape[1089:348564]
dirPaths (
"/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents"
)
docsDir
/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents
2017-07-14 18:26:35.679 IconShape[1089:348564]
file path should be
/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents/iTunesArtwork1024.png
2017-07-14 18:26:35.679 IconShape[1089:348564]
Image file not found
Here is the state of the project after two png files were added.
Yet the log shows they are not visible to NSFileManager. So where would they be found ? i.e. what changes do I need to make to my code in order to find them ?
EDIT
This draft finds the png file following Subramanian's recommendation.
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *imageFilePath = [[NSBundle mainBundle]pathForResource:#"iTunesArtwork1024" ofType:#"png"];
if ([fileManager fileExistsAtPath:imageFilePath])
{
NSLog(#"\nFound file path : %#", imageFilePath);
}
else
{
NSLog(#"\nImage file not found");
}
UIImage *image = [UIImage imageNamed:#"iTunesArtwork1024.png"];
UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
logo.image = image;
[self.view addSubview:logo];
}
The log now reads
Found file path : … .app/iTunesArtwork1024.png
and the image also appears in the subview.
__
Image is not in theDocument Directory, It's inside the bundle.
You have added the image files inside the project. But You are checking the image on Document Directory, Which is wrong. Image is inside app bundle.
You can simply assign the Image to UIImageView by the name of the image.
UIImage *image = [UIImage imageNamed:#"iTunesArtwork1024.png"];
UIImageView *logo = [[UIImageView alloc] init];
logo.image = image;
If you want to get the path of the image, then you have to use [NSBundle mainBundle]
[[NSBundle mainBundle]pathForResource:#"iTunesArtwork1024" ofType:#"png"];
You can access images with names which are added in projects. Try with below code
UIImage *image = [UIImage imageNamed:#"iTunesArtwork1024.png"];
The path where your are looking for the image is inside your device (real or simulator).
To load an image that is stored in your XCode project just do:
UIImage* image = [UIImage imageNamed:#"iTunesArtwork1024.png"];

iOS Objective-C Display PNG Image In Custom Framework

I'm creating a custom framework. Previously in the framework we would download an image and use that to display in a button:
NSString *path = [NSString stringWithFormat: #"https://s3.amazonaws.com/assets/home.png"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *homeButton = [[UIImage alloc] initWithData:data];
self.rewardsCenterButton = [[UIBarButtonItem alloc] initWithImage:[homeButton imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain
target:self
action: #selector(backToRewardsCenter:)];
However, in reviewing this we determined that this isn't good to have as a synchronous call so I'd like to add this PNG to the framework itself.
I've been able to do this by adding the image and ensuring it's included in the copy bundle resources Build Phase. With this set the image gets added in the universal framework output:
However, when I attempt to add this in code, it doesn't seem to show up. Also, when I add the framework in a project, I don't see the image being included, just the headers:
Here's what I've tried so far:
NSString* path = [NSString stringWithFormat:#"%#/TheoremReachSDK.framework/home.png", [[NSBundle mainBundle] bundlePath]];
UIImage *homeButton = [[UIImage alloc] initWithContentsOfFile:path];
And:
NSBundle *bundle = [NSBundle bundleForClass:[TheoremReach class]];
NSString *path = [bundle pathForResource:#"home" ofType:#"png"];
UIImage *homeButton = [[UIImage alloc] initWithContentsOfFile:path];
And:
UIImage *homeButton = [UIImage imageNamed:#"home.png"];
But none of those display anything. Any idea what I need to do to get the image to display?
I'm guessing NSBundle isn't finding the framework via the call to:
[NSBundle bundleForClass:[TheoremReach class]]
Try giving your framework an explicit bundle ID, e.g.: com.theoremreach.sdk, clean your projects and then rebuild.
You can then use code like this to fetch and display your image:
NSString *bundleIdentifier = #"com.theoremreach.sdk";
NSBundle *bundle = [NSBundle bundleWithIdentifier:bundleIdentifier];
if(bundle != nil) {
NSString *path = [bundle pathForResource:#"home" ofType:#"png"];
UIImage *homeButtonImage = [[UIImage alloc] initWithContentsOfFile:path];
if(homeButtonImage != nil) {
self.rewardsCenterButton = [[UIBarButtonItem alloc] initWithImage:[homeButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain
target:self
action: #selector(backToRewardsCenter:)];
} else {
NSLog(#"couldn't find home button image in bundle");
}
} else {
NSLog(#"could not find bundle with identifier %#", bundleIdentifier);
}

ios load an image from docDir into UIimagview - viewDidLoad

I have a group of images saved in my DOcuments folder of the app. I got it to save to that location, I am having trouble loading from that location.
I got the UIimageView working hardcoded with images I dragged to a folder in the project with the code below in the viewDidLoad() method and when i ran the simulator it appeared.
UIImageView *bottomPic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pants.png"]];
bottomPic.frame = CGRectMake(65, 279, 200, 250);
[self.view addSubview:bottomPic];
[bottomPic release];
However when I want to load the image from the Documents Directory nothing loads - Am confused and couldn't find any exact information to help me out on this situation.. This is currently what I have and when I run it doesn't throw any errors but no image appears. Am I missing something in my code? (I'm new to objective-c!)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"bottom1A2A6520-F556-436F-A1BC-D430223DC890-33098-00014708AE7E370F.png"]];
UIImage *image = [UIImage imageWithContentsOfFile:path];
UIImageView *bottomPic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:path]];
bottomPic.frame = CGRectMake(65, 279, 200, 250);
[bottomPic setImage:image];
[self.view addSubview:bottomPic];
[bottomPic release];
A path is a path and an image is an image.
UIImage *image = [UIImage imageWithContentsOfFile:path];
UIImageView *bottomPic = [[UIImageView alloc] initWithImage:image];
Your code has a few issues. Try this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"bottom1A2A6520-F556-436F-A1BC-D430223DC890-33098-00014708AE7E370F.png"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
UIImageView *bottomPic = [[UIImageView alloc] initWithImage:image];
bottomPic.frame = CGRectMake(65, 279, 200, 250);
[self.view addSubview:bottomPic];
[bottomPic release];
Unrelated to the problem, you were needlessly using stringWithFormat:.
The real problem was creating the UIImageView with an unneeded image using imageNamed:.
If this doesn't work then verify that path actually represents a file that really exists in the Documents folder. Verify image isn't nil.
Also consider use ARC (automatic reference counting) to make your life a lot easier.
Update: It appears you needed NSDocumentDirectory instead of NSDocumentationDirectory.

loop to set UIImages from document directory on viewDidLoad

I have 9 buttons whose images are set by the user dynamically. Each button's current image is saved the user documents folder. In viewDidLoad id like to re-set each image to it's UIButton.
I can do this easily enough with:
NSString *filePath = [self documentsPathForFileName: #"boss1.png"];
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *savedBoss = [UIImage imageWithData:pngData];
[boss1Button setImage:savedBoss forState:UIControlStateNormal];
...... 8 more times....
Of course Id prefer to do it using a loop. Only, Im not sure what that would look like in objective C. In jQuery I could do something like:
$('.bosses').each(function( index ) {
var imageUrl='../images/boss'+(index+1)
$('#'+this.id).css('background-image', 'url(' + imageUrl + ')')
});
How can I create a similar objective-C loop that would increment the boss image name and button name similarly?
Furthermore, is there a better way to do this entirely?
I feel like maybe having an NSArray of image urls and an NSArray of UIButton names and pairing them together using a loop might be better.... but again I'm not sure what the syntax would look like for that here.
Try like this and have button tag like 1 to 8
for (id subview in self.view.subviews) {
if([subview isKindOfClass:[UIButton class]]) {
UIButton* button = (UIButton*)subview;
NSString *filePath = [self documentsPathForFileName:[NSString stringWithFromate#"boss%d.png", button.tag]];
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *savedBoss = [UIImage imageWithData:pngData];
[button setImage:savedBoss forState:UIControlStateNormal];
}
}
This will do.
it will be like:
//Add all the buttons in an array for easy loop
NSArray *array = #[btn1,btn2,btn3,btn4... etc];
NSString *name = #"BOSS";
int x = 1;
for(UIButton *btn in array){ // loop through all the buttons
NSString *filePath = [self documentsPathForFileName:[NSString stringWithFormat:#"%#%d.png",name,x]];
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *savedBoss = [UIImage imageWithData:pngData];
[btn setImage:savedBoss forState:UIControlStateNormal];
x++;
}

pathForResource Not Working on ios6

Sorry if this is elementary. Banging my head over it. My app works fine in iOS5.1 and below, but fails to reach my .txt files in my Supporting Files folder in ios6. It always returns "null". I researched this and have checked the Bundle, all is there. The table lists about 8 speakers. On click, it is supposed to pull a DetailView loaded from a .txt file.
...[code]...
else if (self.makeLabel.text == #"Peter Sengenberger") {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"sengenberger" ofType:#"txt"];
if (filePath) {
NSString *myText1 = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding error:nil];
if (myText1) {
textData= [[UITextView alloc] initWithFrame:CGRectMake(5,0, 280,200)]; //size.height-30 )];
textData.text = myText1;
[textData setFont:[UIFont systemFontOfSize:14]];
[textData setBackgroundColor:[UIColor clearColor]];
[textData setTextColor:[UIColor blackColor]];
textData.editable = NO;
textData.dataDetectorTypes = UIDataDetectorTypeLink;
[textScroll addSubview:textData];
}
}
} // end if
Thanks for any help!

Resources