【问题标题】:selecting image for multiple image view from cam or library从凸轮或库中选择多个图像视图的图像
【发布时间】:2013-04-03 08:12:07
【问题描述】:

我的view controller 上有 3 个Image views(用途之前、期间、之后)。 我需要分别为每个视图选择图像。

这个任务的指定方法是:

 -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
      UIImage *image;
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
        [_imgView setImage:image];
        [self dismissViewControllerAnimated:YES completion:NULL];
}

但是如何通知这个方法哪个按钮调用了图像选择器并分别选择/显示视图上的图像。

【问题讨论】:

    标签: ios objective-c image uiimagepickercontroller


    【解决方案1】:

    添加一个“私有”(是的,我不知道这样的事情)ivar 来保存对 selectedImageView 的引用

    @interface MyViewController ()
    
    @property (nonatomic, strong) UIImageView *selectedImageView;
    
    @end
    

    然后选择图像查看时,使用此新IVAR以继续引用当前活动的ImageView

    - (void)imageViewTapped:(id)sender
    {
      // This assumes the use of a gesture recognizer but you can substitute your usual method
      // of detecting the imageView that you want to edit.
      self.selectedImageView = [sender view];
    }
    

    现在你的委托看起来像这样

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
      self.selectedImageView.image = info[UIImagePickerControllerOriginalImage];
      self.selectedImageView = nil;
    
      [self dismissViewControllerAnimated:YES completion:nil];
    }
    

    【讨论】:

    • 我有一个问题,这个 imageViewTapped 什么时候被调用?为什么你 self.selectedImageView= nil ??
    • imageViewTapped: 只是响应触摸的一个示例 - 您可以将其替换为识别用户想要编辑哪个 imageView 的任何方法。我将selectedImageView 设置为nil,因为与它的交互已经完成,所以没有必要保留一个参考,这可能会在以后造成混淆 - 如果还有一些工作要做,你不必这样做那imageView
    • @Paul.s 你在哪里调用 imageviewTapped 方法?
    【解决方案2】:

    在类级别上,获取一个保存图像视图引用的变量。

    当您点击图像时,将点击的图像视图的引用存储到此变量中。

    进入imagepicker委托方法,使用该变量设置图片。

    【讨论】:

    • 我需要更多信息,如果你能提供一些代码就太好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多