Home>

It's nice to be able to save photos taken with the camera, but it comes with a feature that when you select a photo from the library, the selected image is duplicated. I would like to be able to only save photos taken with the camera and turn off this unnecessary duplication function. What should I do?

import UIKit
// Select an image
extension TapViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    // Process when image is selected
    func imagePickerController (_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
        let selectedImage = info [UIImagePickerController.InfoKey.originalImage] as! UIImage
        self.imageView.image = image
        UIImageWriteToSavedPhotosAlbum (image, nil, nil, nil)
        // 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 the imageview according to the size of the image
        if imageHeight>self.view.frame.height || imageWidth>self.view.frame.width {
            print ("1")
            imageWidth = width
            imageHeight = width * heightRatio/widthRatio
        } else if imageHeight>self.view.frame.height {
            print ("2")
            imageHeight = height
            imageWidth = height * widthRatio/heightRatio
        } else if imageWidth>self.view.frame.width {
            print ("3")
            imageWidth = width
            imageHeight = width * heightRatio/widthRatio} else {
        }
        imageView.contentMode = UIView.ContentMode.scaleToFill
        imageView.frame.size = CGSize (width: imageWidth, height: imageHeight)
        // Do not put the image on the navigationbar
        if imageHeight/2>(height/2-navigationBarHeight!) {
            print ("4")
            imageView.center = CGPoint (x: centerX, y: centerY + navigationBarHeight!)
        } else {
            print ("5")
            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)
    }
}