Reputation: 173
I have made an app that saves a screenshot of the device to the app's Documents directory.
This UIImageView then reads in the saved screenshot image and displays it. All good so far. But wait a minute, this works absolutely fine on iOS 8, but it simply doesn't show anything on iOS 7, both simulators and devices.
So I investigated and found that iOS 8 saves the image to the following directory (Simulator on OS X):
/Users/SunburstEnzo/Library/Developer/CoreSimulator/Devices/AE8252CE-38C1-4D81-BA87-34F50E743AEC/data/Containers/Data/Application/34562F4C-5DED-4F1C-9CB6-92948B6C5F89/Documents/2014-11-26-11-12-24.png
While iOS 7 saves it to here:
/Users/SunburstEnzo/Library/Developer/CoreSimulator/Devices/CE8E7B90-F00F-441B-8E70-17E968AB7AF7/data/Applications/994CD541-2730-4E32-93CD-23B9F996C2E3/Documents/2014-11-26-11-12-51.png
I know this has something to do with it but when I reference it, it can see the image fine, it's just having a problem putting it to the screen. And I know what I've done works (on iOS 8) so the code is fine, but I need a workaround for this bug.
Also, whatever my problem is should be similar to this question's problem regarding the change over to using App Containers (NOTE: saving images is fine for me, just displaying it) -- saving image to documents directory no longer works iOS8
Tl;dr bug in iOS 7 stops saved image in Documents directory from showing, works fine on iOS 8, help me work around it
My code:
Writing screenshot to Documents directory: UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
//UIImageWriteToSavedPhotosAlbum(sourceImage,nil, nil, nil);
NSData *pngData = UIImagePNGRepresentation(sourceImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsPath error:&error];
NSString *dateString;
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
dateString = [dateFormatter stringFromDate:now];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",dateString]]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
Code to read:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsPath error:&error];
directoryContentsPopover = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsPath error:&error];
NSLog(@"Number of images: %d",directoryContents.count);
if (directoryContentsPopover.count > 0) {
self.mainImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@/%@",documentsPath,directoryContentsPopover[0]]];
NSLog(@"At least 1 image; Image location: %@/%@",documentsPath,directoryContentsPopover[0]);
}
****Answer by Nick Lockwood****
Replace
self.mainImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@/%@",documentsPath,directoryContentsPopover[0]]];
with
self.mainImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",documentsPath,directoryContentsPopover[0]]];
Upvotes: 1
Views: 463
Reputation: 41005
You are trying to load the image using [UIImage imageNamed:], but this method is only intended for images in your app bundle or XCAssets, it doesn't accept an arbitrary file path.
Use [UIImage imageWithContentsOfFile:] instead.
Upvotes: 1
Reputation: 534
If the image saved on ios8 then it was also get saved in ios7, you are not getting to correct path of documents directory ios8 and ios7 both having different path for document directory
path for ios7 document directory:/Users/**********/Library/Application Support/iPhone Simulator
try this
Upvotes: 0