Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在将图像加载到视图控制器之前,如何裁剪图像_Ios_Swift_Xcode_Uiimageview_Uiimagepickercontroller - Fatal编程技术网

Ios 在将图像加载到视图控制器之前,如何裁剪图像

Ios 在将图像加载到视图控制器之前,如何裁剪图像,ios,swift,xcode,uiimageview,uiimagepickercontroller,Ios,Swift,Xcode,Uiimageview,Uiimagepickercontroller,在我的视图控制器中,我的UIImageView设置为370x370,当我加载图像时,它当前会拉伸以适合ImageView 这是我目前用于拾取图像的功能: func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { // Runs when user is picking image from library i

在我的视图控制器中,我的UIImageView设置为370x370,当我加载图像时,它当前会拉伸以适合ImageView

这是我目前用于拾取图像的功能:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    // Runs when user is picking image from library
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        self.previewImageView.image = pickedImage
        self.selectImageButton.isEnabled = false
        self.selectImageButton.isHidden = true
        uploadImage(image: pickedImage)
        picker.dismiss(animated: true, completion: nil)
    }
}

我是Swift的新手,最好是在代码中实现图像裁剪?

调用ImagePicker函数时,将allowsEditing设置为true:

self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = .camera
self.present(self.imagePicker, animated: true, completion: nil)
另一个选项是,如果需要更多的自定义,可以使用此库:

在didFinishPickingMediaWithInfo中,您可以执行以下操作:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
        dismiss(animated: true, completion: nil)
        let cropVC = TOCropViewController(image: pickedImage)
        cropVC.delegate = self
        cropVC.aspectRatioPickerButtonHidden = true
        cropVC.aspectRatioPreset = .presetSquare
        cropVC.aspectRatioLockEnabled = true
        cropVC.resetAspectRatioEnabled = false
        self.present(cropVC, animated: true, completion: nil)
    }
}

调用ImagePicker函数时,将allowsEditing设置为true:

self.imagePicker.allowsEditing = true
self.imagePicker.sourceType = .camera
self.present(self.imagePicker, animated: true, completion: nil)
另一个选项是,如果需要更多的自定义,可以使用此库:

在didFinishPickingMediaWithInfo中,您可以执行以下操作:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
        dismiss(animated: true, completion: nil)
        let cropVC = TOCropViewController(image: pickedImage)
        cropVC.delegate = self
        cropVC.aspectRatioPickerButtonHidden = true
        cropVC.aspectRatioPreset = .presetSquare
        cropVC.aspectRatioLockEnabled = true
        cropVC.resetAspectRatioEnabled = false
        self.present(cropVC, animated: true, completion: nil)
    }
}

你确定要修剪吗?听起来您确实想将imageView内容模式设置为Aspect Fit。(UIImageView,属性为contentMode,值为aspectFit。)这不是逻辑。确实要裁剪吗?听起来您确实想将imageView内容模式设置为Aspect Fit。(UIImageView,属性为contentMode,值为aspectFit。)这不是逻辑。