How to allow user to pick the image with Swift?

后端 未结 21 803
[愿得一人]
[愿得一人] 2020-12-02 06:01

I am writing my first iOS application (iPhone only) with Swift. The main application view should allow user to choose the image from the photo gallery.

I\'ve found

21条回答
  •  失恋的感觉
    2020-12-02 06:28

    @IBAction func ImportImage(_ sender: Any)
    {
        let image = UIImagePickerController()
        image.delegate = self
    
        image.sourceType = UIImagePickerController.SourceType.photoLibrary
    
        image.allowsEditing = false
    
        self.present(image, animated: true)
        {
            //After it is complete
        }
    
    
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
        {
            myimage.image = image
        }
        else{
            //
        }
        self.dismiss(animated: true, completion: nil)
    
        do {
            try context.save()
        } catch {
            print("Could not save. \(error), \(error.localizedDescription)")
        }
    
    }
    

    Add UINavigationControllerDelegate, UIImagePickerControllerDelegate delegates in the class definition

提交回复
热议问题