【问题标题】:SWIFT | photo gallery not opening斯威夫特 |照片库打不开
【发布时间】:2018-12-26 10:23:59
【问题描述】:

i 创建了一个应用程序菜单,用户可以在其中单击照片,图片显示在 alertView 中,我添加了 2 个操作 -> 一个操作是取消,另一个操作是编辑图像,应该打开图库以选择其他图像。但是,当我单击编辑按钮(已添加代码)时,它什么也不做,并且与关闭警报视图相同。 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)

【问题讨论】:

    标签: ios swift uialertview uitapgesturerecognizer photo-gallery


    【解决方案1】:

    试试这个

    在控制器中创建变量

    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)
        }
    }
    

    【讨论】:

    • 从 taxImagedTappedGesture 方法调用 addActionSheet 函数并告诉我会发生什么?
    • 对成员 'imagePickerController(_:didFinishPickingMediaWithInfo:)' 的不明确引用 - 每个 cameraoption、galleryoption 行和无法使用索引类型为 '[String : Any]' 的值下标UIImagePickerController.InfoKey' - UIIMAGECONTROLLERORIGINALIMAGE 行也出错,由于未在 taximagegesture 方法中声明,因此无法调用该函数
    • 你可以从 info.plist 隐私添加属性列表 - 照片库使用说明和相机使用说明吗?
    • 您使用的是哪个 swift 版本?我的答案是 swift 4.2 和对成员 'imagePickerController(_:didFinishPickingMediaWithInfo:)' 的模糊引用可以解决这个问题 - stackoverflow.com/questions/39760031/…
    • 因为相同的代码,我实现了它与这些功能相机、图库、删除照片和取消的工作正常
    【解决方案2】:

    为相机和画廊创建 NSObject 类。

    import UIKit
    
    class CameraHandler: NSObject{
    
    //MARK:- Shared Instance
    static let shared = CameraHandler()
    private override init() {
    
    }
    
    //MARK:- Variables
    var currentVC: UIViewController!
    
    //MARK: Internal Properties
    var imagePickedBlock: ((UIImage) -> Void)?
    
    //MARK:- Open Camera
    func camera() {
        if UIImagePickerController.isSourceTypeAvailable(.camera){
            UINavigationBar.appearance().barTintColor = UIColor.black
            let myPickerController = UIImagePickerController()
            myPickerController.delegate = self;
            myPickerController.sourceType = .camera
            currentVC.present(myPickerController, animated: true, completion: nil)
        }
    }
    
    //MARK:- Open Gallery
    func photoLibrary() {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
            UINavigationBar.appearance().barTintColor = UIColor.black
            let myPickerController = UIImagePickerController()
            myPickerController.delegate = self;
            myPickerController.sourceType = .photoLibrary
            currentVC.present(myPickerController, animated: true, completion: nil)
        }
    }
    
    //MARK:- ActionSheet For Options
    func showActionSheet(vc: UIViewController) {
        currentVC = vc
        let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    
        actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert:UIAlertAction!) -> Void in
            self.camera()
        }))
    
        actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert:UIAlertAction!) -> Void in
            self.photoLibrary()
        }))
    
        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    
        vc.present(actionSheet, animated: true, completion: nil)
        }
    }
    
     //MARK:- UIImagePickerController Delegate Methods
    extension CameraHandler: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        UINavigationBar.appearance().barTintColor = UIColor.white
        currentVC.dismiss(animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        UINavigationBar.appearance().barTintColor = UIColor.white
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            self.imagePickedBlock?(image)
        }else{
            print("Something went wrong")
        }
        currentVC.dismiss(animated: true, completion: nil)
    }
    }
    

    在您的视图控制器中使用以下代码:

    在当前视图控制器中显示警报:

    CameraHandler.shared.showActionSheet(vc: self)
    

    获取选中的图片:

    CameraHandler.shared.currentVC = self      
    CameraHandler.shared.imagePickedBlock = {(image) in
          print(image)
        }
    

    【讨论】:

    • camerahandler 的声明?
    • CameraHandler 是一个单例类。我已经用 static 关键字声明了。因此,无需再次声明。您只需键入以下内容即可调用任何函数:CameraHandler. shared.FunctionName
    • 但是它给出了这个代码的错误,一个是camerahandler和currentVC的预期声明
    • 你能给我截图吗?这样我就可以检查了。
    • 我不认为我们可以在 cmets 中添加图像,我会对其进行更多研究并在明天回复给您,希望它有效
    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2017-11-13
    • 2015-08-19
    • 2014-09-11
    相关资源
    最近更新 更多