ios - swift |照片库打不开

标签 ios swift uialertview uitapgesturerecognizer photo-gallery

enter image description here我创建了一个应用程序菜单,用户可以在其中单击照片,图片显示在 alertView 中,并且我添加了 2 个操作 -> 其中一个操作正在取消,其他操作正在编辑图像,应该打开图库以选择其他一些图片。但是,当我单击编辑按钮(已添加代码)时,它什么都不做,与关闭警报 View 的工作方式相同。 ALERTVIEW 的代码是

@objc func taxImageTApped(_snder:UITapGestureRecognizer)    {
    print("TaxImage")


    let alertView = UIAlertController(title: "Edit Tax Image", message: "", preferredStyle: UIAlertController.Style.alert)
    let image = #imageLiteral(resourceName: "backimg@3x.png")
    let uiImageAlertAction = UIAlertAction(title: "", style: .default, handler: nil)
    let scaleSze = CGSize(width: 245, height: 245/image.size.width*image.size.height)
    let reSizedImage = image//.resize(newSize: scaleSze)
    uiImageAlertAction.setValue(reSizedImage.withRenderingMode(.alwaysOriginal), forKey: "image")
    alertView.addAction(uiImageAlertAction)
    alertView.addAction(UIAlertAction(title: "Cancel", style:   UIAlertAction.Style.default, handler: nil))
    alertView.addAction(UIAlertAction(title: "Edit", style: .default) {action in

    let newImage = UIImagePickerController()
    newImage.delegate = self
    newImage.sourceType =   UIImagePickerController.SourceType.photoLibrary
    newImage.allowsEditing = false
    })
        self.present(alertView, animated: true, completion: nil)
  }
    /////////////////////below is details for tap gesture i have        applied on //the label    
    taxImageView.translatesAutoresizingMaskIntoConstraints = false
    taxImageView.textColor = tableTextColor
    taxImageView.text = "View Image"
    taxImageView.textAlignment = .left
    taxImageView.attributedText = NSAttributedString(string: "View Image", attributes:
        [.underlineStyle: NSUnderlineStyle.single.rawValue])
    editInfoView.addSubview(taxImageView)
    taxImageView.leftAnchor.constraint(equalTo: editInfoView.leftAnchor, constant: 260).isActive = true
    taxImageView.topAnchor.constraint(equalTo: editInfoView.topAnchor, constant: 740).isActive = true
    taxImageView.widthAnchor.constraint(equalToConstant: 300).isActive = true
    taxImageView.heightAnchor.constraint(equalToConstant: 20).isActive = true
    taxImageView.isUserInteractionEnabled = true
    let taxImageGesture = UITapGestureRecognizer.init(target: self, action: #selector(taxImageTApped))
    taxImageGesture.numberOfTapsRequired = 1
    taxImageGesture.isEnabled = true
    taxImageGesture.cancelsTouchesInView = false
    taxImageView.gestureRecognizerShouldBegin(taxImageGesture)
    taxImageView.addGestureRecognizer(taxImageGesture)

最佳答案

试试这个

在 Controller 中创建变量

let imagePickerController = UIImagePickerController()

在viewDidLoad中添加这个

override func viewDidLoad() {
        super.viewDidLoad()
        imagePickerController.delegate = self
        imagePickerController.allowsEditing = true
        imagePickerController.modalPresentationStyle = .popover

    }

从你想要的地方调用这个函数,或者你也可以添加为手势方法

func addActionSheet() {
    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    let galleryOption = UIAlertAction(title: "Choose Photo", style: .default, handler: { action in
        self.imagePickerController.sourceType = .photoLibrary
        self.present(self.imagePickerController, animated: true, completion: nil)
    })

    let cameraOption = UIAlertAction(title: "Take Photo", style: .default, handler: { action in
        self.imagePickerController.sourceType = .camera
        self.present(self.imagePickerController, animated: true, completion: nil)
    })

    let deleteOption = UIAlertAction(title: "Delete Photo", style: .default, handler: { action in
        self.imageView.image = nil
    })

    let cancelOption = UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
        self.dismiss(animated: true, completion: nil)
    })

    alertController.addAction(galleryOption)
    alertController.addAction(cameraOption)
    alertController.addAction(deleteOption)
    alertController.addAction(cancelOption)
    self.present(alertController, animated: true, completion: nil)
}

添加UIImagePickerControllerDelegateUINavigationControllerDelegate的Delegate方法获取图片

extension ControllerName: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        self.imageView.image = image
        dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
}

关于ios - swift |照片库打不开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53930707/

相关文章:

ios - iOS 中的宏录制

python - Apple 会允许 iOS App Store 中使用 Python 中的 Kivi 框架吗?

ios - UILabel 像字幕一样自动滚动

ios - NSFetchedResultsController 总是包含临时对象

ios - 在 Xcode 上索引文本

swift - 将类型与 Swift 枚举案例关联起来?

ios - 为什么这个委托(delegate)不触发是个谜

Swift - Result.Success 调用中的额外参数

ios - 更改 UIAlertView 中 UITextField 的字体大小

iphone - 在alertView中添加事件指示器