首页 文章

如何从UIImagePickerController获取图像并传递给下一个VC

提问于
浏览
0

我从PhotoLibrary中选择了一张照片,如何实现以下任务

在这种情况下,我使用Swift . 我需要在下一个VC中重建图像:
a)字节
b)图像

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

1)从UIImagePickerController获取图像或字节

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

3)如何获得高度和宽度,以便我可以知道图像是纵向还是横向模式 .

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

在导航到下一个VC之前,我有一个btn点击检查

非常感谢您的帮助

以下是我使用的代码:

@IBOutlet var imageView: UIImageView!

let imagePicker = UIImagePickerController()

 @IBAction func loadImageButtonTapped(sender: UIButton) {
   imagePicker.allowsEditing = false
   imagePicker.sourceType = .PhotoLibrary

  presentViewController(imagePicker, animated: true, completion: nil)
}


//-- - UIImagePickerControllerDelegate Methods

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
   imageView.contentMode = .ScaleAspectFit
   imageView.image = pickedImage
  }

  dismissViewControllerAnimated(true, completion: nil)
}

4 回答

  • 1
    • 您需要将委托分配给图像选择器,以便调用适当的方法 . 在 loadImageButtonTapped 函数中,不要忘记分配图像选择器委托 .

    然后,您应该实现此方法:

    func imagePickerController (_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){ 
            let image = info[UIImagePickerControllerOriginalImage] 
        }
    

    如果你需要找出图像的大小,你可以访问它的 size 属性 .

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

    • 你可以用delegation

    • 当您使用storyboard 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 is deprecated since iOS 3, you should not use that method.

  • 1
    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)
                })
    
  • 0

    1)从UIImagePickerController获取图像

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

    2)将图像传递给其他ViewController在第二个VC中创建UIimage的属性并为其分配图像对象 .

    secondVC.imageProperty = image;
    

    3)获得图像的高度和宽度

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

    改为使用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
    

相关问题