无法在Swift中从ImagePicker裁剪库图像吗?

问题描述 投票:0回答:1

我正在使用图像选择器从图库中拍摄图像并拍照。但是在从图库中选择图像时,我需要裁剪图像。

我能够拾取图像,但是我没有得到作物比例来裁剪图像,为什么?

我为下面的裁剪图像编写了如下代码

   @IBAction func addProfileBtnAction(_ sender: Any) {

    let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    actionSheetController.popoverPresentationController?.sourceView = self.contentView
    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
    }
    actionSheetController.addAction(cancelAction)
    let takePictureAction: UIAlertAction = UIAlertAction(title: "TakePhoto", style: .default) { action -> Void in
        self.openCameraPicker()
    }
    actionSheetController.addAction(takePictureAction)
    let choosePictureAction: UIAlertAction = UIAlertAction(title: "ChooseFromLibrary", style: .default) { action -> Void in
        self.openPhotoGallery()
    }
    actionSheetController.addAction(choosePictureAction)
    self.present(actionSheetController, animated: true, completion: nil)
}
func openCameraPicker() {
    picker.sourceType = UIImagePickerController.SourceType.camera
    picker.cameraCaptureMode = .photo
    picker.allowsEditing = true
    picker.modalPresentationStyle = .fullScreen
    present(picker,animated: true,completion: nil)
}
func openPhotoGallery() {
    picker.sourceType = .photoLibrary
    picker.allowsEditing = true

    picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
    present(picker, animated: true, completion: nil)
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
        imgPick.image = img
    }
    else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        self.imgPick.image = image
    }
    dismiss(animated: true, completion: nil)
}

如何裁剪图像,请提供代码帮助我。

swift uiimagepickercontroller crop image-gallery
1个回答
0
投票

您能从照片库中选择图像吗?

[否则,我想您忘记添加选择器的代表

Picker.delegate =自己

  • 不要忘记在.plist中访问大型照片库
© www.soinside.com 2019 - 2024. All rights reserved.