UIImageView Wont Display Image

Discussion in 'iOS Programming' started by CosmicChild87, Nov 4, 2010.

  1. macrumors newbie

    Joined:
    Nov 4, 2010
    Location:
    England
    #1
    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
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
  3. thread starter macrumors newbie

    Joined:
    Nov 4, 2010
    Location:
    England
    #3
    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
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    imageNamed: does not expect a full path, simply the image name in the resources directory.
     
  5. thread starter macrumors newbie

    Joined:
    Nov 4, 2010
    Location:
    England
    #5
    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?
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  7. thread starter macrumors newbie

    Joined:
    Nov 4, 2010
    Location:
    England
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    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.
     
  9. thread starter macrumors newbie

    Joined:
    Nov 4, 2010
    Location:
    England
    #9
    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
     
  10. macrumors 603

    Joined:
    Aug 9, 2009
    #10
    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.
     
  11. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    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.
     
  12. thread starter macrumors newbie

    Joined:
    Nov 4, 2010
    Location:
    England
    #12
    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
     
  13. dejo, Nov 5, 2010
    Last edited: Nov 5, 2010

    Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    Just a suggestion: use stringByAppendingPathComponent: when constructing a path. It's the preferred method.
     

Share This Page