【问题标题】:Ipad two ImagePickers for one viewIpad 两个 ImagePicker 用于一个视图
【发布时间】:2014-08-12 04:59:08
【问题描述】:

我的 ipad 应用程序的一个视图中有两个图像选择器。一个以弹出框的形式打开照片库,另一个显示相机,并允许用户拍照并将其保存到与照片库相同的 UIImage 中。

我不能使用 didFinishPickingMediaWithInfo 两次,所以我添加了一个基于源类型的 if then 语句作为解决方法。这种方法会导致“if ([picker....line”) 上出现错误“Expected ']'

任何有关如何解决此错误的帮助将不胜感激。

#pragma mark Camera/Library

//Button to open library
- (IBAction)library:(id)sender{

    //Create image picker controller

   self.imagePicker = [[UIImagePickerController alloc]init];

    //Set source to the photo library
   self.imagePicker.delegate = self;
   self. imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
   self.imagePicker.allowsEditing  = NO;

    self.popoverController = [[UIPopoverController alloc]
                                       initWithContentViewController:imagePicker];
   self.popoverController.delegate = self;
   CGRect popoverRect  = [self.view convertRect:[self.view frame]
                                       fromView:[self.view superview]];

   popoverRect.size.width = MIN(popoverRect.size.width, 80) ;
   popoverRect.origin.x = popoverRect.origin.x+150;

    [self.popoverController presentPopoverFromRect:popoverRect inView:self.view
     permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}


-(void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.popoverController != nil) {
        [self.popoverController dismissPopoverAnimated:animated];
        self.popoverController  = nil;
    }
}


- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    self.popoverController=nil;
}


- (IBAction)camera:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeCamera])
    {
     //Create image picker controller

   self.imagePicker = [[UIImagePickerController alloc]init];

   self.imagePicker.delegate = self;
   self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
   self.imagePicker.allowsEditing  = NO;

    self.imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    [self presentViewController:imagePicker animated:YES completion:nil];
    }
    else
    { 
   UIAlertView *alert  = [[UIAlertView alloc] initWithTitle:@"Camera failed to open"
         message:@"Camera is not available" delegate:nil  cancelButtonTitle:@"OK"
         otherButtonTitles: nil];
        [alert show];
    }
}

#pragma mark Image Picker Delegate Methods

//on cancel dimiss picker controller
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//Used when user has chosen an image
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    if ([picker sourceType == UIImagePickerControllerSourceTypeCamera]){
        UIImage * image = info[UIImagePickerControllerOriginalImage];
        imageView.image = image;
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
   UIImage * image = info[UIImagePickerControllerOriginalImage];
   imageView.image  = image;
    }   
 } 

【问题讨论】:

  • if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) 写这个
  • 我仍然在下一行得到预期的 ']' 以及预期的表达式,并在下一行使用未声明的标识符
  • 我将您的代码复制到我的 xcode 并替换该行,它对我来说工作正常
  • 您是否获得了未声明的 imageView 标识符??
  • 实际上我刚刚修复了那个错误,但我仍然得到其他错误

标签: ios objective-c ipad uiimagepickercontroller


【解决方案1】:

您应该遵循以下逻辑。这对您有用,请尝试一次。将按钮命名为 libraryButton 、 cameraButton 并为两个按钮提供相同的 ButtonAction。

在视图控制器.h 文件中

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIButton *libraryButton;
@property (strong, nonatomic) IBOutlet UIButton *cameraButton;
@property (nonatomic, strong) UIImagePickerController *imagePickerController;
@property (nonatomic, strong) UIImage *image1;
@property (strong, nonatomic) IBOutlet UIImageView *myImageView;

@end

并查看控制器 .m 文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize libraryButton;
@synthesize cameraButton;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)ButtonAction:(id)sender{
    self.imagePickerController = [[UIImagePickerController alloc] init];
    self.imagePickerController.delegate = self;

    if (sender == libraryButton) {
        printf("\n library button calling");
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        self.imagePickerController.allowsEditing = YES;
        [self presentViewController:self.imagePickerController animated:YES completion:nil];

    } else if (sender == cameraButton) {
         printf("\n camera button calling");
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self.imagePickerController setAllowsEditing:YES];
            [self presentViewController:self.imagePickerController animated:YES completion:nil];
        } else{
            UIAlertView *alert  = [[UIAlertView alloc] initWithTitle:@"Camera failed to open" message:@"Camera is not available" delegate:nil  cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    picker = nil;
    NSData *imageData = UIImageJPEGRepresentation(image,0.0);
    self.image1 = [UIImage imageWithData:imageData];
    self.myImageView.image = self.image1;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

【讨论】:

  • 所以我将一个按钮映射到按钮操作,然后如何让它知道库和相机按钮已完成?目前我使用了未声明的标识符“librarybutton”
  • 您有 2 个按钮,然后将第一个按钮命名为 libraryButton 并将第二个按钮命名为 cameraButton 。在 ButtonAction 中, sender ==libraryButton 和 sender==cameraButton 将负责您的选择。
  • 是的,这是正确的,所以我仍然在为两个按钮使用未声明的标识符,关于为什么会这样的任何想法?这正是我想要的,谢谢!
  • .h 文件你必须声明 IBOutlet UIButton * labraryButton; IBOutlet UIButton * cameraButton;最后在您的 .Xib 或 Storyboard 文件中连接这两个按钮。
  • 谢谢!那行得通,我不必正确映射 ButtonAction?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 2015-09-15
  • 1970-01-01
  • 2017-03-28
  • 1970-01-01
相关资源
最近更新 更多