I select a photo from PhotoLibrary and How can I achieve below tasks
In this case, I am using Swift. I need to reconstruct the image in the next VC either thru :
loadImageButtonTapped
function, don't forget to assign the image picker delegate.Then, you should implement this method:
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)
}
If you need to find out the size of the image, you cann access it's size
property.
To pass the image to another view controller, you could use one of these two options:
you could use this function for when you navigate from your view controller to another one using 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.
For Swift 4.2
use following
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
}
Try this
dismiss(animated: true, completion: {
self.performSegue(withIdentifier: "Whatever_segue", sender: nil)
})
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)
})
1) Get image from UIImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
//image object
}
2) To pass image to other ViewController Make property of UIimage in Second VC and assign image object to it.
secondVC.imageProperty = image;
3)Get Hight and width of Image
NSLog(@'image height: %f',image.size.height);
NSLog(@'image width: %f',image.size.width);
Use the imagePickerController didFinishPickingImage instead:
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