Home>

I want to be able to make the captured image smaller immediately when I take it with the camera.

Error message

If i use an image taken with an IphoneX camera, the image is clearly displayed on the IphoneX display, but the coordinates and RGB values ​​of the displayed image and ImageView are not the same. Absent.
Therefore, I want to be able to reduce the size of the image taken immediately when taking it with the camera.

Applicable source code

Swift5

import UIKit
class TapViewController: UIViewController {
    @IBOutlet var imageView: UIImageView!
    var image = UIImage ()
    // Tap coordinate variable for the displayed image
    var tapPoint = CGPoint (x: 0, y: 0)
    override func viewDidLoad () {
    }
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
        // Dispose of any resources that can be recreated.
    }
    // Distinguish color when tapping imageview
    @IBAction func getImageRGB (_ sender: UITapGestureRecognizer) {
        guard imageView.image! = nil else {return}
        // Get the tapped coordinates
        tapPoint = sender.location (in: imageView)
        let cgImage = imageView.image? .cgImage!
        let pixelData = cgImage? .dataProvider! .data
        let data: UnsafePointer = CFDataGetBytePtr (pixelData)
        // 1 pixel byte count
        let bytesPerPixel = (cgImage? .bitsPerPixel)!/8
        // number of bytes per line
        let bytesPerRow = (cgImage? .bytesPerRow)!
        print ("bytesPerPixel = \ (bytesPerPixel) bytesPerRow = \ (bytesPerRow)")
        // Calculate the address corresponding to the coordinates of the tapped position
        let pixelAd: Int = Int (tapPoint.y) * bytesPerRow + Int (tapPoint.x) * bytesPerPixel
        // take RGBA values ​​for each
        let r = Int (CGFloat (data [pixelAd]))
        let g = Int (CGFloat (data [pixelAd + 1]))
        let b = Int (CGFloat (data [pixelAd + 2]))
        let a = CGFloat (Int (CGFloat (data [pixelAd + 3])/CGFloat (255.0) * 100))/100
        print ([r, g, b, a])
        // display result in navigationbar
        let R = "R:" + String (Int (r))
        let G = "G:" + String (Int (g))
        let B = "B:" + String (Int (b))
        let A = "A:" + String (format: "% .1f", a)
        navigationItem.title = R + G + B + A
    }

}// Select an image
extension TapViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    // Process when image is selected
    func imagePickerController (_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
        let selectedImage = info [.originalImage] as! UIImage
        // compress the size
        // let resizedImage = selectedImage.scale (byFactor: 0.4)
        image = selectedImage
        var imageHeight = image.size.height
        var imageWidth = image.size.width
        let navigationBarHeight = navigationController? .navigationBar.frame.height
        let width = self.view.frame.width
        let height = self.view.frame.height
        let centerX = self.view.center.x
        let centerY = self.view.center.y
        let widthRatio = imageWidth
        let heightRatio = imageHeight
        // Change the size of iamgeview according to the size of the image
        if imageHeight>self.view.frame.height || imageWidth>self.view.frame.width {
            imageWidth = width
            imageHeight = width * heightRatio/widthRatio
        } else if imageHeight>self.view.frame.height {
            imageHeight = height
            imageWidth = height * widthRatio/heightRatio
        } else if imageWidth>self.view.frame.width {
            imageWidth = width
            imageHeight = width * heightRatio/widthRatio
        } else {
        }
        imageView.contentMode = UIView.ContentMode.scaleAspectFill
        imageView.frame.size = CGSize (width: imageWidth, height: imageHeight)
        // Do not put the image on the navigationbar
        if imageHeight/2>(height/2-navigationBarHeight!) {
            imageView.center = CGPoint (x: centerX, y: centerY + navigationBarHeight!)
        } else {
            imageView.center = CGPoint (x: centerX, y: centerY)
        }
        imageView.image = image
        picker.dismiss (animated: true, completion: nil)
    }

    // Called when shooting is canceled
    func imagePickerControllerDidCancel (_ picker: UIImagePickerController) {
        picker.dismiss (animated: true, completion: nil)
    }
    func tappedlibrary () {
        let sourceType: UIImagePickerController.SourceType =
            UIImagePickerController.SourceType.photoLibrary
        if UIImagePickerController.isSourceTypeAvailable (
            UIImagePickerController.SourceType.photoLibrary) {// create an instance
            let cameraPicker = UIImagePickerController ()
            cameraPicker.sourceType = sourceType
            cameraPicker.delegate = self
            self.present (cameraPicker, animated: true, completion: nil)
        }
        else {
            print ("error")
        }
    }
    func tappedcamera () {
        let sourceType: UIImagePickerController.SourceType =
            UIImagePickerController.SourceType.camera
        // Check if the camera is available
        if UIImagePickerController.isSourceTypeAvailable (
            UIImagePickerController.SourceType.camera) {
            // create an instance
            let cameraPicker = UIImagePickerController ()
            cameraPicker.sourceType = sourceType
            cameraPicker.delegate = self
            self.present (cameraPicker, animated: true, completion: nil)
        }
        else {
            print ("error")
        }
    }
    @IBAction func selecteImageButton (_ sender: UITapGestureRecognizer) {
        // For alert display
        let actionSheet = UIAlertController (title: "", message: "Select photo", preferredStyle: UIAlertController.Style.actionSheet)
        let tappedcamera = UIAlertAction (title: "Shoot with camera", style: UIAlertAction.Style.default, handler: {
            (action: UIAlertAction!) in
            self.tappedcamera ()
        })
        let tappedlibrary = UIAlertAction (title: "Select from library", style: UIAlertAction.Style.default, handler: {
            (action: UIAlertAction!) in
            self.tappedlibrary ()
        })
        let cancel = UIAlertAction (title: "Cancel", style: UIAlertAction.Style.cancel, handler: {
            (action: UIAlertAction!) in
            print ("Cancel")
        })
        actionSheet.addAction (tappedcamera)
        actionSheet.addAction (tappedlibrary)
        actionSheet.addAction (cancel)
        present (actionSheet, animated: true, completion: nil)
    }
}

I tried to resize the image myself, but it didn't work.

Supplemental information (FW/tool version etc.)