如何在 uiimagepickercontroller 中创建可裁剪的横幅图像?
Posted
技术标签:
【中文标题】如何在 uiimagepickercontroller 中创建可裁剪的横幅图像?【英文标题】:How do I create a croppable banner image in uiimagepickercontroller? 【发布时间】:2018-10-22 01:57:56 【问题描述】:当我展示 UIImagePickerController 时,我希望用户能够为用户想要从他们的照片库中导入的横幅裁剪 7.8 的比例。
这个问题类似于许多与创建自定义裁剪矩形相关的问题,这些问题构建为与 UIImagePickerController 一起使用,但所有答案都指向过时的库或过于复杂的库。我想要一些简单的东西。
【问题讨论】:
【参考方案1】:试试这个:
open func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
var newImage: UIImage
if picker.sourceType == .camera
if let possibleImage = info["UIImagePickerControllerOriginalImage"] as? UIImage
// save photo
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad && picker.sourceType == .photoLibrary
if let possibleImage = info["UIImagePickerControllerOriginalImage"] as? UIImage
newImage = possibleImage
else
if let possibleImage = info["UIImagePickerControllerEditedImage"] as? UIImage
newImage = possibleImage
else if let possibleImage = info["UIImagePickerControllerOriginalImage"] as? UIImage
newImage = possibleImage
// logic to manipulate image
print("image size: \(newImage.size)")
let newHeight = 100 // change to preferred height
let scale = 7.8
let newWidth = newImage.size.width
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
newImage.draw(in: CGRect(x: 0, y: 0,width: newWidth, height: newHeight))
newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
print("image size: \(newImage.size)")
// add logic here
【讨论】:
以上是关于如何在 uiimagepickercontroller 中创建可裁剪的横幅图像?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用/用于 UIImagePickerController 显示所有相机控件?
如何在 UIIMagePickerController 中设置自定义图像
如何区分didFinishPickingMediaWithInfo中的UIImagePickerController
如何在 UIImagePickerController 中显示视频或电影文件?