2011-07-11 61 views
0

我嘗試將來自相機或相冊的圖像保存到應用程序目錄中。但我找不到我做錯了什麼。將圖像保存在應用程序文檔中

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
[picker dismissModalViewControllerAnimated:YES]; 
theimageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];  

//obtaining saving path 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"]; 

//extracting image from the picker and saving it 
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
if ([mediaType isEqualToString:@"public.image"]){ 
    UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; 
    NSData *webData = UIImagePNGRepresentation(editedImage); 
    [webData writeToFile:imagePath atomically:YES]; 


    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath]; 
    if(fileExists){ 
     NSLog(@"image Exists"); 
    } 
} 
} 
+0

描述你有什麼問題。 – MarkPowell

+0

爲什麼它返回false?如果我測試該文件是否存在? – Th3noon

+0

我在代碼中看不到任何明顯的錯誤。只需確認文檔目錄中是否有「latest_photo.png」文件即可。 – kthorat

回答

1

爲什麼不使用,

UIImage *orgImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 

哪裏是失敗?你有正確的圖像嗎?或者無法寫入文檔目錄?

+0

你得到的圖像不是問題。我在UIImageView中展示它,但是我不能將它保存在應用程序文檔中。 – Th3noon

+0

Nvm你的偉大是我愚蠢的複製過去的錯誤.. UIImage * orgImage = [info objectForKey:UIImagePickerControllerOriginalImage];做到了:) – Th3noon

+0

@ Th3noon也許你應該接受這個答案?只需點擊分數下方的刻度線即可...... – MCannon

0

檢查文件是否存在於文件資源管理器中。也許編譯器是正確的。

+0

不,我不能找到設備上的圖像.. – Th3noon

+0

thnx你的幫助..我修好了:) – Th3noon

相關問題