Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CosmicChild87

macrumors newbie
Original poster
Nov 4, 2010
13
0
England
Not having a good day today - still trying to learn obj - c and this is my 2nd stupid problem in one day

Im using the following code to load the filename of an image from a database into a UIImage - and from that show it in a UIImage View

Code:
NSArray *documentPaths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDir2 = [documentPaths2 objectAtIndex:0];			 
NSLog(@"Trying to read image from %@",[documentsDir2 stringByAppendingPathComponent:[NSString stringWithFormat:@"%s",sqlite3_column_text(compiledStatement, 2)]]);
UIImage *logoImage = [UIImage imageNamed:[documentsDir2 stringByAppendingPathComponent:[NSString stringWithFormat:@"%s",sqlite3_column_text(compiledStatement, 2)]]];  
NSLog(@"Image filepath %@",[NSString stringWithFormat:@"%s",sqlite3_column_text(compiledStatement, 2)]);
theImageEditView.image = logoImage;
[logoImage release];

In the debugger im getting the following;
2010-11-05 02:41:26.310 Field and Track[94325:207] Trying to read image from /Users/alan/Library/Application Support/iPhone Simulator/4.2/Applications/2A8DFD5D-BC25-437E-A2B2-850BBA53D1C0/Documents/India.JPG
2010-11-05 02:41:26.313 Field and Track[94325:207] Image filepath India.JPG
2010-11-05 02:41:26.314 Field and Track[94325:207] Logo done

and having checked the filepath - the link is right to the image (when running in simulator at least)

The UIImaveView is linked up correctly, and @synthesized but for some reason when i run the code, i get no errors but the image does not display in the ImageView

Thanks in advance

Alan
 

CosmicChild87

macrumors newbie
Original poster
Nov 4, 2010
13
0
England
1) Basically i just want to get it working, and at the moment its only me i know that all the rows in the DB have an image - for now

2)Ok, but that won't stop the image from showing
 

CosmicChild87

macrumors newbie
Original poster
Nov 4, 2010
13
0
England
Ah, thats saving alot of work thanks!
Im sure im doing something really basic wrong but for now im forcing the image to try and load a predifined image just to get it working so im sending it
Code:
UIImage *logoImage = [UIImage imageNamed:@"India.JPG"];  

			///Users/alan/Library/Application Support/iPhone Simulator/4.2/Applications/2A8DFD5D-BC25-437E-A2B2-850BBA53D1C0/Documents/India.JPG
			NSLog(@"Image filepath %@",[NSString stringWithFormat:@"%s",sqlite3_column_text(compiledStatement, 2)]);
			theImageEditView.image = logoImage;

and ive got

Code:
}
.
.
.
	 IBOutlet UIImageView *theImageEditView;
	 IBOutlet UITextField *editTeamName;
	 NSNumber *workingRowID;
	 
	
}


@property(retain,nonatomic) UIImageView *theImageEditView;

in my .h file?
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Make sure you pay attention to chown33's earlier post. Basic debugging is better than "I just want to get it working", because it should reveal why it's not working in the first place. If imageNamed: returns a nil, no image will be shown. If that's the problem, you need to examine why more deeply. Is there no image named "India.JPG"? (Remember, case is significant.) If you're pretty sure that's correct, perhaps it's an issue with imageNamed: not finding the image where it thinks it should be finding it. Etc.
 

CosmicChild87

macrumors newbie
Original poster
Nov 4, 2010
13
0
England
I have done some NSLogs to get to this stage - the information is coming out of the database fine, and "India.JPG" does exist and is stored in the applications documents directory.

I thought that if I hard code the image to the UIImage I am debugging because it will allow me to tell if my problem is with the actual image, or with the UIImage or UIImageView - thats why i did it

To the best of my knowledge now thats where I'm trying to load the image from, or am I wrong?

Thanks again

Alan
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green
I have done some NSLogs to get to this stage - the information is coming out of the database fine, and "India.JPG" does exist and is stored in the applications documents directory.

Neither of which answers the question: Is imageNamed returning nil?

Answering this fundamental question divides the problem into two separately debuggable parts:
  1. If imageNamed returns nil, then the issue lies with loading the image.
  2. If imageNamed returns a non-nil object, then the issue lies with displaying the image.
Remember that you can't display nil, so it is essential to answer this question in order to determine in which half the real problem actually lies.

Also notice that imageNamed doesn't load a specific image at a specific path. If you want that to happen, you're doing it wrong. You need to use imageWithContentsOfFile. Again, you should check its returned value for nil, to ascertain whether it worked or not. If it doesn't load the image, then you have the same two separate debuggable parts: loading the image and displaying the image.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
I think the imageNamed: is a bit of a red-herring in your case since eventually you want to load images from the Documents folder and I'm pretty sure imageNamed: only loads from the application's main bundle. I think you would be better off pursuing using imageWithContentsOfFile: instead.
 

CosmicChild87

macrumors newbie
Original poster
Nov 4, 2010
13
0
England
Thank you dejo, thats where I was going wrong and I've got it working now

Code:
NSString *filePath = [NSString stringWithFormat:@"%@/%s", documentsDir2,sqlite3_column_text(compiledStatement, 2)];
			UIImage *img = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];

did the trick perfectly

Alan
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Just a suggestion: use stringByAppendingPathComponent: when constructing a path. It's the preferred method.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.