将图像保存到文档目录似乎不再适用于iOS8。我听说有Changes To App Containers In iOS 8
。但我似乎仍然无法让我的代码工作。我将图像保存到文档目录,稍后检索以使用。这是我在图像目录中保存图像的代码;
-(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];
}
}
我的documentsPathForFileName
方法;
- (NSString *)documentsPathForFileName:(NSString *)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
return [documentsPath stringByAppendingPathComponent:name];
}
图像路径返回空,但在iOS7中完美运行。