如何在uiimagepickercontroller中创建可裁剪的横幅图像?

时间:2018-10-22 01:57:56

标签: ios swift uiimagepickercontroller

当我呈现UIImagePickerController时,我希望用户能够为用户想要从其照片库导入的横幅裁剪7.8的比例。

这个问题类似于许多与创建与UIImagePickerController一起使用的自定义裁剪矩形有关的问题,但是所有答案都指向过时的库或过于复杂的库。我想要简单的东西。

1 个答案:

答案 0 :(得分:0)

尝试一下:

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
}