来自UIImagePickerController的裁剪图片,如iOS中的信用卡扫描

时间:2017-08-23 01:44:33

标签: ios uiimage uiimagepickercontroller cgimage

我想实现一个可以使用红色CGRect的帧裁剪图像的视图控制器。我尝试了以下代码,但似乎我的裁剪逻辑已经过时了。裁剪后的图像似乎只是原始图像的一小部分......我怎样才能抓住红色矩形CGRect内的任何内容的UII图像?

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        picker.dismiss(animated: true, completion: nil)

        if let x = info[UIImagePickerControllerOriginalImage] as? UIImage {
            let a = x.cgImage?.cropping(to: CGRect(x: 100, y: 100, width: 200, height: 50))
            let b = UIImage(cgImage: a!)
            let y = UIImageView(frame: self.view.frame)
            y.image = b 
            self.view.addSubview(y)
        }
    }

enter image description here

0 个答案:

没有答案