我已编写代码来从相机捕获图像,我想将该图像保存在文档目录中。我已经完成了代码,但我认为图像不存储文档目录。因为当我检索图像时,相机图像不在文档目录中,我的代码是:
- (IBAction)takePhoto {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *cameraImage = info[UIImagePickerControllerEditedImage];
self.tempView.image = cameraImage;
[_chosenImages addObject:_tempView.image];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
id n = [array3 lastObject];
NSLog(@"The id is=%@",n);
NSError *error;
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *dataPath = [aDocumentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",n]];
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSInteger anIndex = 0;
for (UIImage *anImage in _chosenImages) {
NSString *anImageName = [NSString stringWithFormat:@"%d.png", anIndex++];
NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", dataPath, anImageName];
NSLog(@"the path is=%@",anImagePath);
NSData *anImageData = UIImagePNGRepresentation(anImage);
[anImageData writeToFile:anImagePath atomically:YES];
答案 0 :(得分:2)
//在获取NSDocumentDirectory Path时,它将以数组格式提供。所以不要给NSString,而是需要给NSArray ..检查下面的代码
//试试这个
NSArray * aDocumentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [aDocumentsDirectory objectAtIndex:0];
NSString *finaldocPath = [docPath stringByAppendingPathComponent:fileName];