Ios 如何在Swift中裁剪图像?
您好,我正在使用UIImagePicker拍摄图像,但在拍摄图像时,我希望裁剪该图像。。怎么做 我可以从gallery中选择图像到imageview,但在选择图像时,我需要裁剪图像 我尝试了以下代码:Ios 如何在Swift中裁剪图像?,ios,swift,image,camera,crop,Ios,Swift,Image,Camera,Crop,您好,我正在使用UIImagePicker拍摄图像,但在拍摄图像时,我希望裁剪该图像。。怎么做 我可以从gallery中选择图像到imageview,但在选择图像时,我需要裁剪图像 我尝试了以下代码: class AddNewEventViewController: UIViewController, UIPickerViewDelegate,UIImagePickerControllerDelegate, UIPickerViewDataSource,UINavigationControll
class AddNewEventViewController: UIViewController, UIPickerViewDelegate,UIImagePickerControllerDelegate, UIPickerViewDataSource,UINavigationControllerDelegate {
@IBOutlet weak var imgPick: UIImageView!
var picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
hideKeyboardWhenTappedAround()
picker.delegate = self
picker.allowsEditing = true
}
@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)
//Present the
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)
}
}
像这样我得到。。没有收成
在这里,我得到的图像从画廊到imageview,但没有得到作物
如何裁剪图像,请帮助我修改代码。我检查了您的代码,发现您缺少设置
UIImagePickerController的委托
我现在尝试使用photoLibrary
class ImagePickerViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var imageView:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.openPhotoGallery()
}
func openPhotoGallery() {
let picker = UIImagePickerController()
picker.delegate = self
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{
imageView.image = img
}
dismiss(animated: true, completion: nil)
}
}
我检查了您的代码,发现您缺少设置UIImagePickerController
我现在尝试使用photoLibrary
class ImagePickerViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var imageView:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.openPhotoGallery()
}
func openPhotoGallery() {
let picker = UIImagePickerController()
picker.delegate = self
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{
imageView.image = img
}
dismiss(animated: true, completion: nil)
}
}
我还没有加上那个密码。。但是我仍然无法裁剪。。。现在,我编辑了这篇文章,并将其添加到您的VC->UIImagePickerControllerDelegate
,UINavigationControllerDelegate
?请检查我编辑的问题。。刚刚编辑过阿金。。我是否遗漏了任何你可以裁剪的图像。。请输入密码。。任何工作代码。。或者工作样本将是有帮助的,请张贴您的整个代码,或者如果您有在github的样本项目,请共享。。我使用的是Xcode 11.3版本。。我需要代码,请分享我还没有添加代码。。但是我仍然无法裁剪。。。现在,我编辑了这篇文章,并将其添加到您的VC->UIImagePickerControllerDelegate
,UINavigationControllerDelegate
?请检查我编辑的问题。。刚刚编辑过阿金。。我是否遗漏了任何你可以裁剪的图像。。请输入密码。。任何工作代码。。或者工作样本将是有帮助的,请张贴您的整个代码,或者如果您有在github的样本项目,请共享。。我使用的是Xcode 11.3版本。。我需要代码,请分享