Swift 如何在 UIImagepickercontroller 中为编辑器设置编辑器裁剪大小
Swift how to set Editor crop size for editor in UIImagepickercontroller
对于我的 iOS 应用程序,我需要一个 Fotoeditor。因此我设置:
imagePicker.allowsEditing = true
现在我想更改可编辑区域的大小。
这里是可编辑区域,但我需要不同尺寸的可用图片:
如何更改此区域的大小以便为可用图像区域使用不同的大小?
这是我的 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)
}
无法通过 UIImagePickerController 实现,您可以使用此库来实现此功能(GKImagePicker)。
使用UIImagePickerControllerEditedImage
这个代码
例如:-
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage{
preview.contentMode = .scaleAspectFit
preview.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
我试图从图库或相机中保存编辑后的图像,但我在网上找不到任何东西,所以我尝试了这段代码,它成功了! Swift4.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]){}
检查图像的高度和宽度:-
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)
}
对于我的 iOS 应用程序,我需要一个 Fotoeditor。因此我设置:
imagePicker.allowsEditing = true
现在我想更改可编辑区域的大小。
这里是可编辑区域,但我需要不同尺寸的可用图片:
如何更改此区域的大小以便为可用图像区域使用不同的大小?
这是我的 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)
}
无法通过 UIImagePickerController 实现,您可以使用此库来实现此功能(GKImagePicker)。
使用UIImagePickerControllerEditedImage
这个代码
例如:-
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage{
preview.contentMode = .scaleAspectFit
preview.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
我试图从图库或相机中保存编辑后的图像,但我在网上找不到任何东西,所以我尝试了这段代码,它成功了! Swift4.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]){}
检查图像的高度和宽度:-
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)
}