问题
Saving images to the documents directory does not seem to working anymore in iOS8. I had hear that there was Changes To App Containers In iOS 8. But I still cant seem to get my code to work. I save an image to the documents directory, and retrieve it later to use. Here is my code for saving the image in the documents directory;
-(void) simpleCam:(SimpleCam *)simpleCam didFinishWithImage:(UIImage *)image {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//choose Photo
}else{
if (image) {
//save the image to photos library
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
NSData *imageData = UIImageJPEGRepresentation(image,1);
// Get image path in user's folder and store file with name image_CurrentTimestamp.jpg (see documentsPathForFileName below)
NSString *imagePath = [self documentsPathForFileName:[NSString stringWithFormat:@"image_%f.jpg", [NSDate timeIntervalSinceReferenceDate]]];
// Write image data to user's folder
[imageData writeToFile:imagePath atomically:YES];
// Store path in NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:imagePath forKey:[NSString stringWithFormat:@"%@%@",kImageDataKey,self.accessToken]];
// Sync user defaults
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
And my documentsPathForFileName
method;
- (NSString *)documentsPathForFileName:(NSString *)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
return [documentsPath stringByAppendingPathComponent:name];
}
The image path returns empty, but worked perfectly in iOS7.
来源:https://stackoverflow.com/questions/25978067/saving-image-to-documents-directory-no-longer-works-ios8