2017-02-15 117 views
-1

在我的視圖控制器中,我的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) 
    } 
} 

我是相當新的雨燕,這將是最好的是實現圖像裁剪成我的代碼?

+1

你確定你想裁剪?這聽起來像是你真的想將imageView內容模式設置爲Aspect Fit。 (UIImageView,屬性是contentMode,值是aspectFit。) – dfd

+0

這不是邏輯。 –

回答

1

當你調用ImagePicker功能,設置allowsEditing爲真:

self.imagePicker.allowsEditing = true 
self.imagePicker.sourceType = .camera 
self.present(self.imagePicker, animated: true, completion: nil) 

另一種選擇,如果你想多一點的定製,你可以使用這個庫:https://github.com/TimOliver/TOCropViewController

而且在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) 
    } 
}