gpt4 book ai didi

iOS、Swift、UIImagePicker 允许编辑裁剪工具移除透明度。有修吗?

转载 作者:搜寻专家 更新时间:2023-10-31 08:30:55 27 4
gpt4 key购买 nike

当我从 UIImagePicker 获取具有透明背景的 PNG 图像时,它会插入具有透明背景的图像,如下所示:

enter image description here

当我设置时:

imagePicker.allowsEditing = true

它允许我在 UIImagePicker 提供的 View 中按预期裁剪

但是当我按下“选择”时,它会返回黑色而不是透明背景的图像,如下所示:

enter image description here

两个结果之间唯一的变化是设置

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)
}

最佳答案

默认情况下,UIImagePickerController 会移除已编辑图像中的 alpha channel 。要获得这种透明度,您必须自己编辑图像。像这样实现委托(delegate)方法:

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)

}

}

}

关于iOS、Swift、UIImagePicker 允许编辑裁剪工具移除透明度。有修吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41873885/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com