Updated 28 February 2022
Hello guys, Today we learn about Image Resizing In Swift.
In this blog, we see how to resize the image when we upload the image from the Gallery or Camera by using an image picker.
Sometimes we need to upload fixed height and width images from the gallery or camera when we take the image from the media it will be in some fixed size and we want to take some fixed size of the image, At that time we need to use the image resizing function.
1 2 3 4 |
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0) image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() |
Getting Started:- Let’s take an example, for this, I am creating a design in my storyboard.
Step1:- Firstly, create a new Xcode project
File–> New–> Project –> Ios –> next And Add your project name then create.
Step2:- Secondly, create a UI Screen on the storyboard. here I am taking image View and two text fields, one for image width and the second one for image height, and the upload image button for uploading the image from the gallery.
Then, create an outlet in the view control class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class ViewController: UIViewController { @IBOutlet weak var widthTF: UITextField! @IBOutlet weak var image: UIImageView! @IBOutlet weak var heightTf: UITextField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func uploadPicTBtnapp(_ sender: Any) { } } |
Step3:- Now, Add the code for the functionality.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import UIKit class ViewController: UIViewController { @IBOutlet weak var widthTF: UITextField! @IBOutlet weak var image: UIImageView! @IBOutlet weak var heightTf: UITextField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func uploadPicTBtnapp(_ sender: Any) { let picker = UIImagePickerController() picker.sourceType = .photoLibrary picker.delegate = self picker.allowsEditing = false self.present(picker, animated: true, completion: nil) } } extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{ func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { // Local variable inserted by Swift 4.2 migrator. var width = "" var height = "" if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage { if widthTF.text != ""{ width = widthTF.text! }else{ width = "100" } if heightTf.text != ""{ height = heightTf.text! }else{ height = "100" } let widthValue = Int(width) ?? 381 let heightValue = Int(height) ?? 492 let scaledImage: UIImage = imageValue(with: image, scaledTo: CGSize(width: widthValue, height: heightValue)) self.image.image = scaledImage } else { print("Something went wrong") } picker.dismiss(animated: true, completion: nil) } func imageValue(with image: UIImage, scaledTo newSize: CGSize) -> UIImage { UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0) image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() // drawingImageView.image = newImage return newImage ?? UIImage() } } |
Here we are using an image picker controller to upload the image, To know about the working of Image picker functionality Please click here.
Now, run the code and see the output
In this blog, we discussed Image Resizing In Swift.
I hope this blog will help you to understand the working of Image Resizing.
Thanks for reading!!!
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.