问题:
即使设置为true:
imagePicker.allowsEditing = true
仅显示裁剪功能,适用于iPhone大小的设备但适用于iPad大小的设备(裁剪无法正常工作,返回错误的部分图片)。
如何启用“照片”的编辑功能,以便用户可以使用此裁剪功能?
完成方法:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
isImageChanged = true
if UIDevice.current.userInterfaceIdiom == .pad {
//Code for iPad problem
pictureBox.image = image
}else{
pictureBox.contentMode = .scaleAspectFit
pictureBox.image = image
}
self.dismiss(animated: true, completion: nil)
}
调用ImagePickerController
let alertAction2 = UIAlertAction(title: "Library", style:UIAlertActionStyle.default){(UIAlertAction)-> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
if UIDevice.current.userInterfaceIdiom == .pad {
imagePicker.allowsEditing = true
} else if UIDevice.current.userInterfaceIdiom == .phone {
imagePicker.allowsEditing = true
}
self.present(imagePicker, animated: true, completion: nil)
}
}
答案 0 :(得分:0)
我遇到了同样的问题,并且有一个简单的解决方案。在iPad上使用imagePickerController时,您必须将其作为弹出窗口呈现,以便编辑正常工作。 Source
以下是一些图片,用于说明编辑工作。
接下来,我们在iPhone上遇到了popOver的问题。弹出窗口无法在iPhone上显示,因此它只显示占据整个屏幕的普通选择器。请注意,您可能还想检查是否可以访问照片库或相机。 (代码在上面提供的链接中。)
@IBAction func selectImagePressed(_ sender: Any) {
// check if we can access the library first? If no we need to ask again
picker.allowsEditing = true
picker.sourceType = .photoLibrary // can be camera or library
picker.modalPresentationStyle = .popover
picker.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate
picker.popoverPresentationController?.sourceView = view
let screenSize = UIScreen.main.bounds
let xLocation = (screenSize.width / 100) * 15
let yLocation = (screenSize.height / 2)
picker.popoverPresentationController?.sourceRect = CGRect(x: xLocation, y: yLocation, width: 0, height: 0)
present(picker, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
// Use editedImage Here
self.imageView.image = editedImage
nextButton.isHidden = false
}
else if let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
// Use originalImage Here
self.imageView.image = originalImage
nextButton.isHidden = false
}
else {
if let vc = UIApplication.topViewController() {
Helper.showAlertMessage(vc: vc, title: "Image Error", message: "An error occured during image selection")
}
}
picker.dismiss(animated: true)
}