2013-07-29 118 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];