iOS、Swift、UIImagePicker 允许编辑裁剪工具移除透明度。有修吗?
iOS, Swift, UIImagePicker allows editing crop tool removes transparency. Is there fix?
当我从 UIImagePicker 获取具有透明背景的 PNG 图像时,它会插入具有透明背景的图像,如下所示:
当我设置:
imagePicker.allowsEditing = true
它允许我在 UIImagePicker 提供的视图中按预期裁剪
但是当我按下 'Choose' 它 returns 图像是黑色而不是透明背景,如下所示:
两个结果之间的唯一变化是设置
imagePicker.allowsEditing = true
并在应用程序中裁剪图像。
有没有办法在裁剪时保持透明度或至少将黑色更改为白色?
我在互联网上搜索过,没有提到我能看到的。
代码:
func showImagePicker() {
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
compImageView?.contentMode = .scaleAspectFit
compImageView?.backgroundColor = UIColor.clear
compImageView?.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
请尝试此代码。
import MobileCoreServices //importing for mediaType kUTTypeImage
func showImagePicker() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
let mediaTypes: Array<AnyObject> = [kUTTypeImage]
imagePicker.mediaTypes = mediaTypes as! [String]
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if picker.sourceType == UIImagePickerControllerSourceType.photoLibrary {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
dismiss(animated: true, completion: nil)
compImageView?.image = image
}
}
}
默认情况下,UIImagePickerController 会移除已编辑图像中的 alpha 通道。要获得这种透明度,您必须自己编辑图像。像这样实现委托方法:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let cropRect = info[UIImagePickerControllerCropRect] as? CGRect {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
let imageRef: CGImage = image.cgImage!.cropping(to: cropRect)!
let image1: UIImage = UIImage(cgImage: imageRef, scale: image.scale, orientation: image.imageOrientation)
imageView.image = image1
picker.dismiss(animated: true, completion: nil)
}
}
}
当我从 UIImagePicker 获取具有透明背景的 PNG 图像时,它会插入具有透明背景的图像,如下所示:
当我设置:
imagePicker.allowsEditing = true
它允许我在 UIImagePicker 提供的视图中按预期裁剪
但是当我按下 'Choose' 它 returns 图像是黑色而不是透明背景,如下所示:
两个结果之间的唯一变化是设置
imagePicker.allowsEditing = true
并在应用程序中裁剪图像。
有没有办法在裁剪时保持透明度或至少将黑色更改为白色?
我在互联网上搜索过,没有提到我能看到的。
代码:
func showImagePicker() {
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
compImageView?.contentMode = .scaleAspectFit
compImageView?.backgroundColor = UIColor.clear
compImageView?.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
请尝试此代码。
import MobileCoreServices //importing for mediaType kUTTypeImage
func showImagePicker() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
let mediaTypes: Array<AnyObject> = [kUTTypeImage]
imagePicker.mediaTypes = mediaTypes as! [String]
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if picker.sourceType == UIImagePickerControllerSourceType.photoLibrary {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
dismiss(animated: true, completion: nil)
compImageView?.image = image
}
}
}
默认情况下,UIImagePickerController 会移除已编辑图像中的 alpha 通道。要获得这种透明度,您必须自己编辑图像。像这样实现委托方法:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let cropRect = info[UIImagePickerControllerCropRect] as? CGRect {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
let imageRef: CGImage = image.cgImage!.cropping(to: cropRect)!
let image1: UIImage = UIImage(cgImage: imageRef, scale: image.scale, orientation: image.imageOrientation)
imageView.image = image1
picker.dismiss(animated: true, completion: nil)
}
}
}