2011-10-05 43 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'。 –