Can't get image to appear in imageView


Have been searching my code over and over, and cannot find what I have done wrong. I am new to objective-c (and in fact, programming in total) and am not comfortable with the whole debugger/breakpoints thing.

Anyway, my problem is that I cannot get images (both taken with camera on my device, and taken from photo library in iphone simulator) to end up on the imageView I created. I post my code here and hope that someone can find what I cannot find. So in advance, big thanks to anyone who is helping me out. It will be much appreciated!


[code]#import <UIKit/UIKit.h>

@class BNRItem;

@interface DetailViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate>
__weak IBOutlet UITextField *nameField;
__weak IBOutlet UITextField *serialNumberField;
__weak IBOutlet UITextField *valueField;

__weak IBOutlet UILabel *dateLabel;
__weak IBOutlet UIImageView *imageView;

@property (nonatomic, strong) BNRItem *item;

  • (IBAction)takePicture:(id)sender;
  • (IBAction)backgroundTapped:(id)sender;



[code]#import “DetailViewController.h”
#import “BNRItem.h”
#import “BNRImageStore.h”

@interface DetailViewController ()


#import “BNRItem.h”

@implementation DetailViewController

@synthesize item;

  • (void)setItem:(BNRItem *)i
    item = i;
    [[self navigationItem] setTitle:[item itemName]];

  • (IBAction)takePicture:(id)sender
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    // If our device has a camera, we want to take a picture, otherwise, we just pick from the photo library
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    } else {
    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

    [imagePicker setDelegate:self];
    // Place image picker on the screen
    [self presentViewController:imagePicker


  • (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
    NSString *oldKey = [item imageKey];

    //Did the item already have an image?
    if (oldKey) {
    // Delete the old image
    [[BNRImageStore sharedStore] deleteImageForKey:oldKey];

    // Get picked image from info dictionary
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    // Create a CFUUID object - it know how to create unique identifier strings
    CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);

    // Create a string from unique identifier
    CFStringRef newUniqueIDString = CFUUIDCreateString (kCFAllocatorDefault, newUniqueID);

    // Use that unique ID to set our item’s imageKey
    NSString *key = (__bridge NSString *)newUniqueIDString;
    [item setImageKey:key];

    // Store image in the BNRImageStore with this key
    [[BNRImageStore sharedStore] setImage:image
    forKey:[item imageKey]];


    [imageView setImage:image];

    // Take image picker off the screen - your must call this dismiss method
    [self dismissViewControllerAnimated:YES completion:nil];

  • (BOOL)textFieldShouldReturn:(UITextField *)textField
    [textField resignFirstResponder];
    return YES;

  • (IBAction)backgroundTapped:(id)sender
    [[self view] endEditing:YES];

#pragma mark - Overriden methods

  • (void)viewDidLoad
    [super viewDidLoad];
    [[self view] setBackgroundColor:[UIColor groupTableViewBackgroundColor]];

  • (void)viewWillAppear:(BOOL)animated
    [super viewWillAppear];

    [nameField setText:[item itemName]];
    [serialNumberField setText:[item serialNumber]];
    [valueField setText:[NSString stringWithFormat:@"%d", [item valueInDollars]]];

    // Create a NSDateFormatter that will turn a date into a simple data string
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    // Use filtered NSDate object to set dateLabel contents
    [dateLabel setText:[dateFormatter stringFromDate:[item dateCreated]]];

    NSString *imageKey = [item imageKey];

    if (imageKey) {
    // Get image for image key from image store
    UIImage *imageToDisplay = [[BNRImageStore sharedStore] imageForKey];
    // Use that image to put on the screen in imageView
    [imageView setImage:imageToDisplay];
    } else {
    // Clear the imageView
    [imageView setImage:nil];

  • (void)viewWillDisappear:(BOOL)animated
    [super viewWillDisappear];

    // Clear first responder
    [[self view] endEditing:YES];

    // “Save” changes to item
    [item setItemName:[nameField text]];
    [item setSerialNumber:[serialNumberField text]];
    [item setValueInDollars:[[valueField text] intValue]];



[code]#import <Foundation/Foundation.h>

@interface BNRImageStore : NSObject
NSMutableDictionary *dictionary;

  • (BNRImageStore *)sharedStore;
  • (void)setImage:(UIImage *)i
    forKey:(NSString *)s;
  • (UIImage *)imageForKey:(NSString *)s;
  • (void)deleteImageForKey:(NSString *)s;



[code]#import “BNRImageStore.h”

@implementation BNRImageStore

  • (id)allocWithZone:(NSZone *)zone
    return [self sharedStore];

  • (BNRImageStore *)sharedStore
    static BNRImageStore *sharedStore = nil;
    if (!sharedStore) {
    // Create singleton
    sharedStore = [[super allocWithZone:NULL] init];
    return sharedStore;

  • (id)init
    self = [super init];
    if (self) {
    dictionary = [[NSMutableDictionary alloc] init];

    return self;

  • (void)setImage:(UIImage *)i
    forKey:(NSString *)s
    [dictionary setObject:i forKey:s];

  • (UIImage *)imageForKey:(NSString *)s
    return [dictionary objectForKey:s];

  • (void)deleteImageForKey:(NSString *)s
    if (!s)
    [dictionary removeObjectForKey:s];




Hi Oscar,

I have looked over the code you have supplied and cannot see a problem myself.

Are you getting any errors?

Have you hooked up the UIImageView to your DetailViewController correctly?



Haha Nick, you did it again! What would I do without you? :slight_smile:
Thanks for saving a mortal objective-c beginner from hours of frustration and error searching!


No problem Oscar, glad you got it sorted.


Thanks a lot for this! I’m looking for this solution