【问题标题】:How to get image from UIImagePickerController and Pass to next VC如何从 UIImagePickerController 获取图像并传递给下一个 VC
【发布时间】:2015-08-14 23:22:06
【问题描述】:

我从 PhotoLibrary 中选择了一张照片,我该如何完成以下任务

在这种情况下,我使用的是 Swift。我需要通过以下方式在下一个 VC 中重建图像:
a) 字节
b) 图片

通过使用 Segue 或用户类。如果 Image 文件很大,最好通过 segue 或 class 传递。

1) 从 UIImagePickerController 获取 Image 或 Bytes

2) 如何将图像字节传递给下一个 VC
使用 Segue
或使用类
我是否使用用户类来存储图像或字节?

3) 如何获取高度和宽度,以便知道图像是纵向还是横向模式。

4) 如何检查ImageView中是否有图片?

在导航到下一个 VC 之前,我有一个要单击的 btn 进行检查

非常感谢您的帮助

以下是我使用的代码:

@IBOutlet var imageView:UIImageView! 让 imagePicker = UIImagePickerController() @IBAction func loadImageButtonTapped(sender: UIButton) { imagePicker.allowsEditing = false imagePicker.sourceType = .PhotoLibrary presentViewController(imagePicker,动画:真,完成:无) } //--- - UIImagePickerControllerDelegate 方法 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject: AnyObject]) { 如果让pickImage = info[UIImagePickerControllerOriginalImage] 作为? UIImage { imageView.contentMode = .ScaleAspectFit imageView.image =pickImage } 解除视图控制器动画(真,完成:无) }

【问题讨论】:

标签: ios swift


【解决方案1】:

试试这个

dismiss(animated: true, completion: {
        self.performSegue(withIdentifier: "Whatever_segue", sender: nil)
    })

【讨论】:

    【解决方案2】:
    1. 您需要将委托分配给您的图像选择器,以便调用适当的方法。在您的 loadImageButtonTapped 函数中,不要忘记分配图像选择器委托。

    那么,你应该实现这个方法:

        func imagePickerController (_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){ 
            // check if an image was selected, 
            // since a images are not the only media type that can be selected
            if let img = info[.originalImage] {
                methodToPassImageToViewController(img)
        }
    

    如果你需要知道图片的大小,你可以访问它的size属性。

    1. 要将图像传递给另一个视图控制器,您可以使用以下两个选项之一:

      • 你可以使用delegation
      • 当您使用故事板 segues 从视图控制器导航到另一个视图控制器时,您可以使用此功能:

        func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) 
        {
             var destinationController = segue.destinationViewController
             // now you can pass the image to the destination view controller
        }
        

    P.S: didFinishPickingImage 自 iOS 3 起已弃用,您不应使用该方法。

    【讨论】:

    • 在 Swift 4 上:func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    【解决方案3】:

    对于 Swift 4.2

    使用关注

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
    
    }
    

    【讨论】:

      【解决方案4】:

      改用 imagePickerController didFinishPickingImage:

      1 & 2)

          // Class variable
          var image: UIImage?
          ...
          func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 
      
              self.image = image
              picker.dismissViewControllerAnimated(true, completion: { (finished) -> Void in
                  // Perform your segue to your next VC
              })
          }
      
         override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      
      // One of the two segue.destinationViewController.isKindOfClass(YourDestinationViewController) {
          if segue.identifier == "Your Destination View Controller Identifier" {
                  let destinationViewController = segue.destinationViewController as! YourDestinationViewController
                  destinationViewController.image = self.image
              }
          }
      

      3)

      image.size.height
      image.size.width
      

      【讨论】:

      • 我需要先显示用户选择的内容。如果正确,则导航到下一个 VC。你的意思是我必须实现 didFinishPickingMediaWithInfo 和 didFinishPickingImage ?
      • 不,如果“我需要显示用户首先选择的内容”是指您需要向用户显示图像以供他们批准,那么这已经在 ImagePickerController 中完成。您使用 didFinishPickingImage 来获取用户正在选择的 UIImage 视图,然后当您对新视图控制器执行 segue 时,您使用 prepareForSegue 传递它。
      【解决方案5】:

      1) 从 UIImagePickerController 获取图片

      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
          //image object       
      }
      

      2) 将图像传递给其他ViewController 在Second VC 中制作UIimage 的属性并将图像对象分配给它。

      secondVC.imageProperty = image;
      

      3)获取图片的高度和宽度

      NSLog(@'image height: %f',image.size.height);
      NSLog(@'image width: %f',image.size.width);
      

      【讨论】:

        【解决方案6】:
            picker.dismissViewControllerAnimated(true, completion: { () -> Void in
                        let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
        
             // Convert image to data and Send data anotherViewController 
        
               image data 
        
              UIImageJPEGRepresentation(self.pickedImage, 0.5)
                    })
        

        【讨论】:

        • 我需要让用户在导航到下一个 VC 之前检查它是否正常。你的意思是让 imageData = UIImageJPEGRepresentation(self.pickedImage,0.5) 吗?这是图像还是字节数据类型?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-22
        • 2019-05-08
        • 2016-10-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多