nserrorinitwithcontentsoffile

How do I add a NSError check with initWithContentsOfFile when looking for an image?


In my app, the user can save an image to their documents directory. At launch, I grab the image, add a border, and put it into a UIImageview like this....

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
        NSString *docDirectory = [sysPaths objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/ImageOne.jpg", docDirectory];
        UIImage *unborderedImage  = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];

        //image found....add border
        UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
        imageOneView.image = imageWithBorder;

Ideally, I like to check that the image is there first before adding a border. If not, load an "image not available" placeholder. Something like this:

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
        NSString *docDirectory = [sysPaths objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/ImageOne.jpg", docDirectory];
        UIImage *unborderedImage  = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];

NSError * error;

    if (error != nil) {
        //image found....add border
        UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
        imageOneView.image = imageWithBorder;
    } else
        //no image saved
        [imageOneView setImage:[UIImage imageNamed:@"photoNotAvailable.png"]];
    }

Of course, this doesn't work. I just can't seem to figure out how handle if "ImageOne.jpg" isn't found.


Solution

  • As it turns out, I need to do other things with the image elsewhere within the app. This would also depend on whether or not the user had saved an image or not. So in my method where the user can save the image, I send out a NSNotification that the image has changed. Then on my MainView, I look for the notification and key off that.

    When saved:

    [collectionOneUserDefinedDefaults setObject:@"image added" forKey:@"collectionOneImageAdded"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"collectionOneImageChanged" object:self];
    

    Then in my MainView I look for the notification:

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCollectionOneImage) name:@"collectionOneImageChanged" object:nil];
    
    - (void)updateCollectionOneImage {
    
        //check if an image was ever saved, if so, replace the noPhotoAvailalble placeholder
        NSUserDefaults *collectionOneUserDefinedDefaults = [NSUserDefaults standardUserDefaults];
        NSString *collectionOneImageTextString = [collectionOneUserDefinedDefaults stringForKey:@"collectionOneImageAdded"];
    
        if (collectionOneImageTextString == nil || [collectionOneImageTextString isEqualToString:@""]) {
            [collectionOneImage setImage:[UIImage imageNamed:@"photoNotAvailable.png"]];
        }
        else {
            //pull in collection one image from the Documents folder
            NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
            NSString *docDirectory = [sysPaths objectAtIndex:0];
            NSString *filePath = [NSString stringWithFormat:@"%@/CollectionOneImage.jpg", docDirectory];
            UIImage *unborderedImage  = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];
            //image found....add border
            UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
            collectionOneImage.image = imageWithBorder;
        }
    }
    

    It works perfectly. It in turn builds in the error handling. If the user saved and image, it gets loaded. If not, a placeholder image is loaded.