SteveSTL
SteveSTL

Reputation: 998

This used to work: Displaying image using imageWithContentsOfFile

This was working for me yesterday morning and now it doesn't. So, I suspect something else changed to cause it...but I can't find the change. I've spent hours reverting my code back almost a week and still it's not working (and I know it was working yesterday morning). So, I'm hoping that in posting this specific issue (a symptom?) some ideas will surface that I can evaluate. Thanks.

I download images as they're needed:

NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *targetFile = [NSString stringWithFormat:@"%@/%@.%@", documentDirectory, imageName, imageType];

// only download those where an image exists
if(![imageType isEqualToString:@""])
{
    // only download the file if there is not already a local copy.
    if([filemgr fileExistsAtPath:targetFile] == NO)
    {
        NSMutableData *imageData = [[NSMutableData alloc] initWithLength:0];
        [imageData appendData:data];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *thumbNailFilename = [NSString stringWithFormat:@"%@.%@", imageName, imageType];
        NSString *thumbNailAppFile = [documentsDirectory stringByAppendingPathComponent:thumbNailFilename];
    }
}

Then display them:

NSString *imageFullName = [NSString stringWithFormat:@"%@%@", [greetingObject valueForKey:@"gid"], [greetingObject valueForKey:@"itp"]];
NSString *fullImagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:imageFullName];
UIImage *greetingImage = [UIImage imageWithContentsOfFile:fullImagePath];
self.greetingImage.image = greetingImage;

The variables "imageFullName" and "fullImagePath" are populated with the correct data and the image files are present on the simulator in the specified directory. Yet, "greetingImage" equals nil.

Here's what I get for "fullImagePath": /Users/Steve2/Library/Application Support/iPhone Simulator/7.1/Applications/8C9F8417-F6E2-4B38-92B3-82A88477CB7F/Documents/165.jpg

Here are the image files: screen shot of the images at the specified path

I have also tried variations using initWithContentsOfFile and dataWithContentsOfFile and get the same result. The greetingImage variable is nil.

I appreciate your ideas. I've even reinstalled Xcode in hopes that something got corrupted. No dice.

Added: One thing I just thought of... I did add the SystemConfiguration.framework to the project yesterday for an unrelated feature. It's currently at the top of the Linked Frameworks and Libraries list. I have no experience working with this. Could it be causing the problem?

Thanks.

Upvotes: 2

Views: 205

Answers (1)

jbutton
jbutton

Reputation: 379

Your code looks correct.

I would check that the images themselves are still okay. Looking at the screenshot you posted Finder isn't showing previews of the images which it should do with a valid JPEG. You say that the images are being downloaded so I suspect that they are being corrupted somehow on the way down.

EDIT:

Didn't notice that you were using initWithContentsOfFile. Since you are saving the files as NSData objects you will need to load them into memory as NSData objects and then init a UIImage with the data object, like so:

 NSData *imageData = [NSData dataWithContentsOfFile:filePath];
 [UIImage imageWithData:imageData];

Upvotes: 1

Related Questions