ios - 如何在 swift 4 中的自定义 UIImagePickerController 中设置按钮。

标签 ios swift uiimagepickercontroller

这是我的代码。我正在使用自定义 imagepickercontroller 进行图像操作,例如裁剪、旋转、压缩和图像质量。我想向 imagepickercontroller 添加一个自定义按钮。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
        let str=info[UIImagePickerControllerImageURL] as Any
        let name=(str as! URL).lastPathComponent
        imageName=name
        let fixOrientationImage=pickedImage.fixedOrientation()
        //profileImg.image = fixOrientationImage
        self.imageProfile.contentMode = .scaleToFill
        self.imageProfile.image=fixOrientationImage
       // self.imageProfile.image = pickedImage
    }
    dismiss(animated: true, completion: nil)
}

如何从 imagePickerController 中的自定义按钮调用此函数 pickedImage.fixedOrientation()

UIImage 扩展

func fixedOrientation() -> UIImage {
    if imageOrientation == .up { return self }

    var transform:CGAffineTransform = .identity
    switch imageOrientation {
    case .down, .downMirrored:
        transform = transform.translatedBy(x: size.width, y: size.height).rotated(by: .pi)
    case .left, .leftMirrored:
        transform = transform.translatedBy(x: size.width, y: 0).rotated(by: .pi/2)
    case .right, .rightMirrored:
        transform = transform.translatedBy(x: 0, y: size.height).rotated(by: -.pi/2)
    default: break
    }

    switch imageOrientation {
    case .upMirrored, .downMirrored:
        transform = transform.translatedBy(x: size.width, y: 0).scaledBy(x: -1, y: 1)
    case .leftMirrored, .rightMirrored:
        transform = transform.translatedBy(x: size.height, y: 0).scaledBy(x: -1, y: 1)
    default: break
    }

    let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height),
                        bitsPerComponent: cgImage!.bitsPerComponent, bytesPerRow: 0,
                        space: cgImage!.colorSpace!, bitmapInfo: cgImage!.bitmapInfo.rawValue)!
    ctx.concatenate(transform)

    switch imageOrientation {
    case .left, .leftMirrored, .right, .rightMirrored:
        ctx.draw(cgImage!, in: CGRect(x: 0, y: 0, width: size.height,height: size.width))
    default:
        ctx.draw(cgImage!, in: CGRect(x: 0, y: 0, width: size.width,height: size.height))
    }
    return UIImage(cgImage: ctx.makeImage()!)
}

最佳答案

 //create btn add set properties
    let btn = UIButton(type: .custom)
    btn.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    btn.addTarget(self, action: #selector(customBtnTapped(_:)), for: .touchUpInside)

    //set bar button item
    let barbutton = UIBarButtonItem(customView: btn)
    self.navigationItem.leftBarButtonItem = barbutton

自定义按钮操作方法

@objc fileprivate func customBtnTapped(_ sender: UIButton) {
    let img = self.fixedOrientation()
}

关于ios - 如何在 swift 4 中的自定义 UIImagePickerController 中设置按钮。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49205852/

相关文章:

iphone - 集成 facebook ios api 时出现 clang 错误

swift - 使用 Alamofire result 和 present() 切换 View

ios - 在 CoreData Swift 中存储 bool 值的正确方法

swift - 如何使用segue firebase发送数据?

ios - 如何通过 iOS 应用程序从设备拍照?

iOS UIImagePickerController 前置闪光灯

ios - 如何使空的 UITextView 像 iOS 上的 Notes 一样可滚动?

ios - xcode - 如何提取由字符分隔的字符串的一部分“

ios - 如何让标准的 iPhone Copy 气泡出现在 UIImage 上?

ios - 如何从 UIImagePicker 检测图像是否为横向