2011-10-05 44 views
0

我正在iPad上開發此應用程序。
它有一個功能,允許用戶通過NSDocumentDirectory從iPad的照片庫存儲圖像到應用程序。XCode - 如何顯示允許用戶鍵入值的消息框?

在第一個屏幕中有一個瀏覽按鈕,確認按鈕和一個imageview。
瀏覽按鈕將顯示iPad照片庫中的照片,並允許用戶選擇照片。 確認按鈕將選定的照片存儲到NSDocumentDirectory中。

瀏覽按鈕:

- (IBAction) browsePhoto:(id)sender 
{ 
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 
    imagePickerController.delegate = self; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePickerController]; 
    [popover setPopoverContentSize:CGSizeMake(320,320)]; 
    [popover presentPopoverFromRect:CGRectMake(200,200,-100,-100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    self.popoverController = popover; 
    [imagePickerController release]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo 
{ 
    [self.popoverController dismissPopoverAnimated:YES]; 
    imageView.image = selectedImage; 
} 

確認按鈕:

- (IBAction) confirmPhoto:(id)sender 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 
    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:@"SavedImage.png"]; 
    UIImage *image = imageView.image; 
    NSData *imageData = UIImagePNGRepresentation(image); 
    [imageData writeToFile:savedImagePath atomically:NO]; 
} 

我使用這些代碼嘗試在一個圖像,所以將工作完美的罰款。 但過了一段時間,我發現我必須允許用戶存儲多張照片。我的代碼無法做到這一點,因爲我以某種方式'硬編碼'的文件名是SavedImage.png,新的圖像將簡單地替換舊的圖像。

我想到了一種方法來解決這個問題,它通過使用計數器來命名圖像,當我將它們存儲到我的應用程序中時。 類似的東西:

NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", counter]]; 

但是,這不會是我的圖片了良好的命名約定。我想用專名保存它們。我想到的是,當用戶點擊確認按鈕時,應用程序將顯示一個消息框,提示並允許用戶鍵入所選圖像的名稱。圖像將被保存爲用戶輸入的名稱。

回答

0

爲OK和ABORT創建一個帶有UITextField和2個按鈕的ViewController,並用UIPopOverController顯示它。你應該知道,你必須檢查合法的文件名,檢查已經存在的文件(並允許覆蓋)。

+0

謝謝你的建議。但你介意分享一些代碼來檢查文件名是否已經存在於NSDocumentDirectory中? – Lloydworth

+0

請參閱NSFileManager類引用以及一大堆方法,例如'fileExistsAtPath'。 –