Iphone XCode-如何显示允许用户输入值的消息框?
我正在iPad上开发这个应用程序。 它有一个功能,允许用户通过NSDocumentDirectory将iPad照片库中的图像存储到应用程序中 第一个屏幕中有一个浏览按钮、确认按钮和一个图像视图。 浏览按钮将显示iPad照片库中的照片,并允许用户选择照片。 “确认”按钮将所选照片存储到NSDocumentDirectory中 浏览按钮:Iphone XCode-如何显示允许用户输入值的消息框?,iphone,objective-c,ios,xcode,ipad,Iphone,Objective C,Ios,Xcode,Ipad,我正在iPad上开发这个应用程序。 它有一个功能,允许用户通过NSDocumentDirectory将iPad照片库中的图像存储到应用程序中 第一个屏幕中有一个浏览按钮、确认按钮和一个图像视图。 浏览按钮将显示iPad照片库中的照片,并允许用户选择照片。 “确认”按钮将所选照片存储到NSDocumentDirectory中 浏览按钮: - (IBAction) browsePhoto:(id)sender { UIImagePickerController *imagePickerContr
- (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]];
但对于我的图像来说,这不是一个好的命名约定。我想用专有名称保存它们。我想到的是,当用户点击确认按钮时,应用程序将显示一个消息框,提示并允许用户输入所选图像的名称。然后,图像将保存为用户输入的名称。创建一个带有UIExtField和2个“确定”和“中止”按钮的小型ViewController,并使用UIPopOverController显示它。您应该知道,您必须检查合法的文件名,检查已有的文件并允许覆盖。谢谢您的建议。但是,您介意共享一些代码来检查文件名是否已经存在于NSDocumentDirectory中吗?请参阅NSFileManager类参考,其中包含一整套方法,如fileExistsAtPath。