iOS7 : What is the best way to give the user the ability to choose between taking a photo and going to a photo gallery?

标签 ios iphone objective-c image uiimagepickercontroller

当用户单击某个按钮时,我希望他能够在拍照和从图库中选择图片之间进行选择。截至目前,它似乎是其中之一。我应该为此创建两个按钮还是有更好的方法来完成这个?我希望有一种点击按钮的方式,iOS 将在 native 为用户提供选择。

   -(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];
 }

最佳答案

在我看来,最好的方法是创建例如 2 个按钮

  • UIBarButtonItem * choosePhotoBtn
  • UIBarButtonItem * TakePhotoBtn

然后在按下任一按钮时调用相同的 IBAction,并在其中添加条件以调用适当的接口(interface),如下所示:

-(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];
}

But these are the best options, I like the UIAlertView the most to be honest

老实说,这对用户来说是更多的点击次数,但这也是我的看法。 以下是您可以如何做到这一点,创建您的 UIAlertView ,然后在其委托(delegate)方法中放置一个条件:

-(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];

}

关于iOS7 : What is the best way to give the user the ability to choose between taking a photo and going to a photo gallery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20356296/

相关文章:

objective-c - MKNetworkKit:请求链,其中每个后续请求都需要来自前一个请求的数据

python - 如何在我的 Swift 软件应用程序中为我的函数计算运行 Python 代码?

html - 移动 CSS 仅在设备旋转后加载

iphone - 在 iOS 上使用 FB SDK 将视频上传到 Facebook,会删除方向。我如何解决它?

objective-c - XCode:应用程序->iPod 运行/构建不上传应用程序

iphone - 如何实现initWithObjects?

ios - 如何防止用户选择字段时 UIPickerView 关闭?

iphone - 应用程序在启动时崩溃,仅更新而不是全新安装

ios - 无法从Web动态将图像设置为imageview

ios - AudioQueueReset是释放音频缓冲区还是仅返回它们?