简体   繁体   English

iOS7:让用户能够在拍照和进入图片库之间进行选择的最佳方法是什么?

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

When a user clicks a certain button,I want him to have the ability to choose between taking a picture and choosing a picture from a gallery. 当用户单击某个按钮时,我希望他能够在拍照和从图库中选择图片之间进行选择。 As of right now it seems to be one or the other. 截至目前,似乎是其中之一。 Should I create two buttons for this or is there a better what to accomplish this? 我应该为此创建两个按钮,还是有更好的方法来完成此操作? I am hoping a way to click a button and iOS will natively give the user the choice. 我希望可以通过单击按钮的方式来实现,而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];
 }

In my opinion the best way would be to Create for example 2 buttons 我认为最好的方法是创建例如2个按钮

  • UIBarButtonItem * choosePhotoBtn
  • UIBarButtonItem * TakePhotoBtn

Then call the same IBAction when pressing either button, and put a condition in it to call the appropriate interface somehting like this: 然后在按下任一按钮时调用相同的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];
}

But these are the best options, I like the UIAlertView the most to be honest 但这是最好的选择,老实说,我最喜欢UIAlertView

To be honest thats more clicks for the user, but again thats my opinion . 老实说,这是用户获得更多点击的机会,但这又是我的看法。 Here's how you can do it, Create your UIAlertView , then put a condition in its deleguate method: 这样做的方法是,创建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];

}

When user press the button you can display UIAlertView or UIActionSheet, I would recommended UIAlertView for two button. 当用户按下按钮时,可以显示UIAlertView或UIActionSheet,我建议为两个按钮使用UIAlertView。 And in the delegate method base on user selection you can make your logic to access camera or gallery. 在基于用户选择的委托方法中,您可以使自己的逻辑访问摄像机或画廊。

There are two options - 有两种选择-

  1. First show the pickerController with source type as photo library. 首先显示带有源类型作为图片库的pickerController。 Then there is a property in UIImagePickerController called cameraOverlayView. 然后,UIImagePickerController中有一个名为cameraOverlayView的属性。 You can set it to a custom view with the button at the bottom that on click switches the pickerController source type to camera. 您可以使用底部的按钮将其设置为自定义视图,单击该按钮可以将pickerController的源类型切换为camera。

  2. Read the photo library and present them for selection in a custom view with a button to launch camera which would use the UIImagePickerController with sourceType as camera. 阅读照片库,并通过按钮启动相机,将其显示在自定义视图中以供选择,以启动相机,该相机将使用UIImagePickerController和sourceType作为相机。

More about overlayView - [UIImagePickerController cameraOverlayView] 有关overlayView的更多信息- [UIImagePickerController cameraOverlayView]

Sample code project from Apple - Using UIImagePickerController to Select Pictures and Take Photos 苹果公司的示例代码项目- 使用UIImagePickerController选择图片和拍照

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM