2009-08-12 37 views
2

UIImagePickerView控制器返回圖像的NSDataUIImagePickerView控制器 - 圖像路徑 - iphone

我的要求是對圖像的路徑存儲爲varchar數據類型。

UIImagePickerView中選擇圖像後,如何獲取iPhone照片庫所選圖像的完整路徑?

我的應用程序不用擔心在我的應用程序中存儲圖像。這些圖像已存儲在iPhone的照片庫中。

在此先感謝。

回答

7

這是不可能的。 UIImagePickerController會給你一個UIImage*,你必須自己將它轉換成NSData,並將它存儲在你的本地沙箱中。沒有SDK認可的方式來訪問用戶照片庫中的圖像(出於安全原因)。

假設你使用的是SDK 3.0,這裏有一個選擇器的返回函數,它將它保存到磁盤,在應用程序的文檔目錄中(這應該可行,儘管我可能在某處有一個小錯誤):

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    // Dismiss the picker 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 

    // Get the image from the result 
    UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"]; 

    // Get the data for the image as a PNG 
    NSData* imageData = UIImagePNGRepresentation(image); 

    // Give a name to the file 
    NSString* imageName = @"MyImage.png"; 

    // Now, we have to find the documents directory so we can save it 
    // Note that you might want to save it elsewhere, like the cache directory, 
    // or something similar. 
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* documentsDirectory = [paths objectAtIndex:0]; 

    // Now we get the full path to the file 
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName]; 

    // and then we write it out 
    [imageData writeToFile:fullPathToFile atomically:NO]; 
} 
+0

這意味着 - 圖像被複制 - 圖像存儲在兩個地方,在我的應用程序和在iPhone中,是這樣嗎? – 2009-08-12 22:59:37

+1

是的。不幸的是,蘋果公司沒有提供一種獲取原始圖像的方法。您可以通過手動查看用戶的照片庫(它位於/ private中)並嘗試查找匹配(昂貴)的圖像來非正式地進行此操作。無論如何,如果您將圖像作爲NSData存儲在磁盤上,即使是巨大的圖像(幾百萬像素)也不應占用太多空間。所以在現實中,這很少是一個問題。 – Itay 2009-08-12 23:16:20

+0

你能幫我嗎,如何將UIImage存儲到我的應用程序? – 2009-08-12 23:16:57