BlueSpud
BlueSpud

Reputation: 1623

Loading UIImage From File Doesn't Work, Does on Simulator


So I've been trying to take an image from the photo library and save it to the app container, then load it. Heres my code, its very simple:

Creating the banner folder:

    //create the banner folder
[[NSFileManager defaultManager] createDirectoryAtPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingString:@"/Banners"] withIntermediateDirectories:NO attributes:NULL error:NULL];

Saving the image:

 UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

//if (banner != nil)
//    [[NSFileManager defaultManager]removeItemAtURL:banner error:NULL];


long time = mach_absolute_time();
NSLog(@"saved %i",[UIImagePNGRepresentation(image) writeToFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingString:[NSString stringWithFormat:@"/Banners/%lu.png",time]] atomically:YES]);

banner = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingString:[NSString stringWithFormat:@"/Banners/%lu.png",time]];

//finally dismiss the picker
[picker dismissViewControllerAnimated:YES completion:NULL];

And then later on, loading it:

UIImageView* bannerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (CLASS_VIEW_SIZE + CLASS_VIEW_SPACE)*i, self.view.frame.size.width, CLASS_VIEW_SIZE+30)];
        UIImage* banner = [UIImage imageWithContentsOfFile:[dict objectForKey:@"banner"]];
        [bannerView setImage:banner];
        [scrollView addSubview:bannerView];

I would expect that to work. When the file is saved when the app is open, everything works fine. When I close the app, it doesn't load back on the actual device. Running this is the simulator works fine. As an additional note, in Xcode, going to devices, clicking on my app and going to the Documents folder, I can see that the file isn't there. I've tried checking if the file exists with NSFileManager, but it says it doesn't exist, even though it is there.

Has something changed in the iOS 8.1 API that makes this not work? I pretty much just copied and pasted code from some of my old projects.

Thanks.

EDIT: After a bit more testing, I noticed it only stops working when I stop the app from Xcode and then launch it from Xcode. Closing it out and opening on the actual device seems fine, but that still doesn't explain the oddity.

Upvotes: 0

Views: 731

Answers (2)

BlueSpud
BlueSpud

Reputation: 1623

So I figured out what was causing my problem. I was saving the absolute path to the image, which included the document directory. For example:

/var/mobile/Containers/Data/Application/1196D7EF-2791-41F5-BC53-B685873ACFBC/Documents/Banners/5873637476238.png

However, when Xcode builds the app and runs it, it changes the numbers and letters for the application, so basically the path component after Application/, would then become invalid, because it would have changed. This is why when I didn't build it, the numbers didn't change. I realized that on the simulator, I just launched the app, instead of rebuilding it.

The same thing would probably happen if the app was updated. The solution to this problem is to use relative paths, instead of the whole path, just save the name of the file. Then get the document directory at run time and append the file name you saved onto it.

Hope this helps someone in the future.

Upvotes: 2

Walt Sellers
Walt Sellers

Reputation: 3939

With the results of so many steps cascading to the inputs of so many others, it is difficult to say which part failed. For the sake of debug ability, it is better to break them up into individual steps.

If the symptoms are specific to when the app is killed suddenly by Xcode, it is difficult to rule out some kind interruption being the cause.

But since your users will not be killing the app with a debugger, it should not be a problem.

To diagnose how the debugger kill could cause the issue, we would have to know more about the code.

Upvotes: 1

Related Questions