Swift如何在UIImagepickercontroller中为编辑器设置编辑器裁剪大小

时间:2018-04-05 14:13:28

标签: ios swift uiimagepickercontroller

对于我的iOS应用程序,我需要一个Fotoeditor。因此我设置:

imagePicker.allowsEditing = true

现在我想更改可编辑区域的大小。

这是可编辑区域,但我需要可用图像的大小不同:

image

如何更改此区域的大小以使用不同大小的可用图像区域?

这是我的PickerController的代码

@IBOutlet weak var preview: UIImageView!

let imagePicker = UIImagePickerController()

@IBAction func selectImage(_ sender: Any) {
    imagePicker.allowsEditing = true
    imagePicker.sourceType = .photoLibrary

    present(imagePicker, animated: true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    imagePicker.delegate = self
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
        preview.contentMode = .scaleAspectFit
        preview.image = pickedImage
    }
    dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

4 个答案:

答案 0 :(得分:0)

通过UIImagePickerController无法实现,您可以使用此库来实现此功能(GKImagePicker)。

答案 1 :(得分:0)

我试图从库或相机中保存编辑后的图像,但在网上找不到任何内容,因此我尝试使用此代码,它可以正常工作!雨燕4.2

if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
            imageProfile.contentMode = .scaleAspectFit
            imageProfile.image = pickedImage
}

这行代码必须在函数内部

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){}

答案 2 :(得分:0)

检查图像的高度和宽度:-

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let image = info[.editedImage] as? UIImage else {return}
        imageView.image = image
        dismiss(animated: true, completion: nil)
        print(image.size.height)
        print(image.size.width)
        print(image.size)
    }

答案 3 :(得分:-1)

使用package main import ( "log" "net/http" "os/exec" "time" ) func main() { http.HandleFunc("/", myHandler) go func() { <-time.After(100 * time.Millisecond) err := exec.Command("explorer", "http://127.0.0.1:8080").Run() if err != nil { log.Println(err) } }() log.Println("running at port localhost:8080") log.Fatal(http.ListenAndServe(":8080", nil)) } 此代码 例如: -

UIImagePickerControllerEditedImage