2

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.

SteveSTL
  • 998
  • 3
  • 13
  • 21
  • Have you double checked that the file exists manually by doing into the Documents directory? – Nicholas Smith Aug 19 '14 at 16:12
  • Yes. I can see the file in Finder at the path in my question. I'll add the screen shot. – SteveSTL Aug 19 '14 at 16:15
  • Most likely your images are simply broken - not correctly downloaded/saved. To check this try to replace 165.jpg with any other jpg manually downloaded from the internet. – Nekto Aug 20 '14 at 04:54
  • @Nekto - I replaced the image with one I can open on my Mac and the app opens it as well. However, I understood the best way to store images were as NSData objects. I used the advice here ([link](http://stackoverflow.com/questions/17443562/ios-best-way-to-store-the-downloaded-images)) to download and then display the image. Is this not the correct/best way to download and store images? Thanks. – SteveSTL Aug 20 '14 at 16:03
  • @SteveSTL that questions should be another question on SO. I would accept Joshua Button answer. – Nekto Aug 20 '14 at 17:39

1 Answers1

1

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];
jbutton
  • 380
  • 2
  • 8
  • thanks @Joshua Button. Please see my comment in response to Nekto's. – SteveSTL Aug 20 '14 at 17:27
  • If the images shown above are NSData objects written to disk then using +imageWithContentsOfFile: will not work. What you're looking for is [+imageWithData](https://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/clm/UIImage/imageWithData:) – jbutton Aug 20 '14 at 18:26
  • 1
    JoshuaButton and @Nekto you were right. The NSData was corrupted. An error was occurring on the server and was being prepended to the image file. The error has been fixed and the code in the question works. Thanks. – SteveSTL Aug 20 '14 at 19:41