我试图从UIImagePicker中裁剪一个图像到一个特定的矩形,但我收到的是深色而不是图像(这似乎是图像的颜色主题)。
首先是我的imagePicker函数。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let originalImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
imagePicker.dismiss(animated: true, completion: nil)
if originalImage == nil { print("Error Not Found An Image")}
imageView.image = originalImage
let croppedImage = cropImage(image: originalImage, rect: CGRect(x: 0.13, y: 0.12, width: 0.74, height: 0.75))
imageView.image = croppedImage
}
还有我的裁剪函数
func cropImage (image: UIImage, rect: CGRect) -> UIImage {
let resImage = CIImage(image: image, options: [:])
let croppedImage = resImage?.cropped(to: rect)
let img = UIImage(ciImage: croppedImage!, scale: 1, orientation: image.imageOrientation)
return img
}
问题出在矩形的值上。在我的例子中,它们实在是太小了。
谢谢badhanganesh的提示。