0

For the past 5 or 6 months i have been saving images to a Photos folder in which i created within the documents directory on the users device. The photos would then be loaded into an imageView when the user entered a specific screen, until recently this has been working perfectly.

Now the images no longer show in the image view and when using the URL to print out the image width and height, the values are zero even though the URL has not changed.

I am using the following code to display the image within the image view.

outputImageView = [[UIImageView alloc] init];

    [outputImageView setFrame:originalImageViewSize];

    outputImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:photoURL]];

    NSLog(@"width %f, height %f", outputImageView.image.size.width, outputImageView.image.size.height);

the photoURL is as follows: /Users/fernandosantos/Library/Developer/CoreSimulator/Devices/6F3FCE2A-F7F9-4F77-B66D-3DA2A25BE166/data/Containers/Data/Application/C3867EFA-6AA8-4B71-A856-DCF7F83DEFF0/Documents/AUDIBURY/Photos/COMMTRCIAL-CSRSignature--2014-10-13 10-49-16.jpg

Thanks

3
  • I'm guessing [NSData dataWithContentsOfFile:photoURL] is returning nil. Are you seeing this issue with iOS 8 Specifically? If so.. You need to show us how you are getting the photoURL. In iOS 8 the directory structure for your sandbox has changed, if you're not using the correct APIs to get the file path then the photoURL will be incorrect. See my answer here for more info about the changes in iOS 8: stackoverflow.com/a/25885321/814389 Commented Oct 13, 2014 at 10:38
  • I'm receiving the photoURL from coreData like this: AnswerSignature *signature = [results objectAtIndex:0]; photoURL = signature.signatureURL; Commented Oct 13, 2014 at 10:39
  • First thing is to go and check iPhone Simulator folder, whether the file is exist there or not. Commented Oct 13, 2014 at 10:50

2 Answers 2

2

In iOS8 the application documents directory dynamically changes its name on every running. You have to use the following method from Appdelegate in order to get the current documents directory path:

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

You should only save the relative path of the file, not the full path. This method also works on iOS7.

You can use the iOS simulator and check that the full path actually doesn't exist.

Sign up to request clarification or add additional context in comments.

6 Comments

Okay, i have made sure i am getting the current documents directory by using the above method and appending my custom folder created along with the image name. The image is still not loading into the image view yet the image is there within the iPhone simulator directory.
So my current photoURL is the following: file:///Users/fernandosantos/Library/Developer/CoreSimulator/Devices/3B3123A9-724B-45D4-8705-0ABC9E87A589/data/Containers/Data/Application/EE56A436-7C72-4B0B-A9BA-940BD211ED8E/Documents/AUDIBURY/Photos/COMMTRCIAL - CSRSignature--2014-10-13 13-19-15.jpg
I'm not sure, but you may need to replace those spaces " " with "%20" with stringByReplacingOccurrencesOfString:@" " withString:@"%20", I'm not sure but you can try it. Moreover, I would try in a real device if you can
If you are using the simulator, you can access that path through terminal in order to check that the path actually exists
Thanks for the help, i managed to fix my problem. I will upload an answer later when i have a minute. using the above methods gives you the url of the photo with "file://" at the beginning which will not work.
|
0

Thanks to everyone that replied with a suggestion.

I combined what everyone had suggested and came up with this solution which works for me.

//get the documents directory, in iOS 8 this is now dynamic everytime you run the app.

 NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

//Create the CurrentPhotoURL by using the current documents directory and the appending the photo name which
//was created when saving the image originally.
NSString *currentPhotoURL = [[documentsDirectory stringByAppendingString:@"/"] stringByAppendingString:photoName];

Since i had created a new a folder within the documents directory called Photos, the photoName variable holds the name of the Photos folder followed by the name of the image when it was originally saved.

I then append the photoName variable to the current documents directory to get the full directory path for that photo.

This may not be the best solution but its what i came up with and it works.

If anyone else needs further information then please contact me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.