当用户点击某个按钮时,我希望他能够在拍照和从图库中选择图片之间进行选择。截至目前,它似乎是一个或另一个。我应该为此创建两个按钮还是有更好的方法来实现这一目标?我希望有一种方法可以点击按钮,iOS会自动为用户提供选择。
-(IBAction)takePhoto:(id)sender
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
// image picker needs a delegate,
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
}
答案 0 :(得分:1)
在我看来,最好的方法是创建例如2个按钮
UIBarButtonItem * choosePhotoBtn
UIBarButtonItem * TakePhotoBtn
然后在按下任一按钮时调用相同的IBAction,并在其中放入一个条件来调用适当的界面,如下所示:
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIBarButtonItem *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentViewController:picker animated:YES completion:nil];
}
但这些是最好的选择,我最喜欢UIAlertView
说实话,用户可以获得更多点击次数,但这再次证明了我的看法。 以下是如何执行此操作,创建UIAlertView,然后在其deleguate方法中添加条件:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *BtnSelected = [alertView buttonTitleAtIndex:buttonIndex];
if([BtnSelected isEqualToString:@"choosePhotoBtn"] {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentViewController:picker animated:YES completion:nil];
}
答案 1 :(得分:0)
当用户按下按钮可以显示UIAlertView或UIActionSheet时,我建议使用UIAlertView两个按钮。在基于用户选择的委托方法中,您可以使您的逻辑访问相机或图库。
答案 2 :(得分:0)
有两种选择 -
首先显示具有源类型的pickerController作为照片库。然后在UIImagePickerController中有一个名为cameraOverlayView的属性。您可以将其设置为自定义视图,单击底部的按钮可将pickerController源类型切换到摄像机。
阅读照片库,并在自定义视图中显示它们,并使用按钮启动相机,该相机将使用带有sourceType的UIImagePickerController作为相机。
有关overlayView的更多信息 - [UIImagePickerController cameraOverlayView]
Apple的示例代码项目 - Using UIImagePickerController to Select Pictures and Take Photos