2013-07-29 62 views
1

我已经编写了从摄像机捕获图像的代码,我想将该图像保存在文档目录中。我已经做了代码,但我认为该图像不存储文档目录。因为当我获取图像的摄像机图像不在文档目录,我的代码:未保存在文档目录中的摄像机图像

- (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

这是什么说法给你,的NSLog(@“路径=%@”,anImagePath) ;和NSLog anImageData来检查数据是否为空 – satheeshwaran

+0

我在iPhone上测试我不知道.. – Nasir

+0

我认为问题不在于存储代码..但是有了取回代码..您能否展示如何获取来自文件目录的图像? –

回答

2

//在拿NSDocumentDirectory路径,它将在阵列形式给予。因此,而不是给予的NSString你需要给的NSArray。检查下面的代码

//试试这个

NSArray * aDocumentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docPath = [aDocumentsDirectory objectAtIndex:0]; 
NSString *finaldocPath = [docPath stringByAppendingPathComponent:fileName];