【问题标题】:iOS How to load photo from camera to parse SwiftiOS 如何从相机加载照片以解析 Swift
【发布时间】:2018-04-13 05:31:54
【问题描述】:

如何从相机或手机库中加载照片进行解析,例如 PFFile?

如何从我知道的资产中加载图像,我的代码:

    func loadImage() {
       let query = PFQuery(className: "_User")

    query.findObjectsInBackground { (objects, error) in
        let firstObject = objects?.first as PFObject?
        let objectFile = firstObject?.object(forKey: "avatar") as! PFFile
        objectFile.getDataInBackground(block: { (imageData, error) in
            let image = UIImage(data: imageData!)
            if image != nil {
                self.avatar.image = image
            }
        })
    }

}

此代码从资产上传图片。 但我需要从相机或图书馆上传。

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    试试这个。 点击按钮时调用displayUploadImageDialog func。它将打开对话框,并且当您从照片中选择任何图像时,delegate 方法调用之外的任何图像。

    func displayUploadImageDialog(btnSelected: UIButton) {
            let picker = UIImagePickerController()
            picker.delegate = self
            picker.allowsEditing = true
            let alertController = UIAlertController(title: "", message: "Action on Upload", preferredStyle: .actionSheet)
            let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: "Cancel action"), style: .cancel, handler: {(_ action: UIAlertAction) -> Void in
                alertController.dismiss(animated: true) {() -> Void in }
            })
            alertController.addAction(cancelAction)
            let takePhotoAction = UIAlertAction(title: NSLocalizedString("Take Photo", comment: "Take Photo action"), style: .default, handler: {(_ action: UIAlertAction) -> Void in
                if UI_USER_INTERFACE_IDIOM() == .pad {
                    OperationQueue.main.addOperation({() -> Void in
                        picker.sourceType = .camera
                        self.present(picker, animated: true) {() -> Void in }
                    })
                }
                else {
                    if !UIImagePickerController.isSourceTypeAvailable(.camera) {
                        let passwordAlert = UIAlertController(title: "Error", message: "Device has no camera", preferredStyle: .alert)
                        let yesButton = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in
                            //Handel your yes please button action here
                            passwordAlert.dismiss(animated: true) {() -> Void in }
                        })
                        passwordAlert.addAction(yesButton)
                        self.present(passwordAlert, animated: true) {() -> Void in }
                    }
                    else {
                        picker.sourceType = .camera
                        self.present(picker, animated: true) {() -> Void in }
                    }
                }
            })
            alertController.addAction(takePhotoAction)
            let cameraRollAction = UIAlertAction(title: NSLocalizedString("Camera Roll", comment: "Camera Roll action"), style: .default, handler: {(_ action: UIAlertAction) -> Void in
                if UI_USER_INTERFACE_IDIOM() == .pad {
                    OperationQueue.main.addOperation({() -> Void in
                        picker.sourceType = .photoLibrary
                        self.present(picker, animated: true) {() -> Void in }
                    })
                }
                else {
                    picker.sourceType = .photoLibrary
                    self.present(picker, animated: true) {() -> Void in }
                }
            })
            alertController.addAction(cameraRollAction)
            alertController.view.tintColor = Colors.NavTitleColor
            present(alertController, animated: true) {() -> Void in }
        }
    
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        var user = PFUser.current()
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        let imageData = UIImageJPEGRepresentation(image, 0.05)
        let imageFile = PFFile(name:"image.jpg", data:imageData!)
        user!["profilePicture"] = imageFile;
        user?.saveInBackground(block: nil)
    
        self.dismiss(animated: true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }
    

    【讨论】:

    • 无法使用“String”类型的索引为“[NSObject : AnyObject]”类型的值下标
    • 我可以运行这个函数吗? (来自按钮或其他东西?)
    • 这些不是函数,这些是UIImagePickerController的委托方法。如果您想在按钮上执行代码,请单击参考更新的答案。
    • 实际上这一行是检查当前设备是iPhone还是iPad。如果您的应用程序仅针对 iPhone 开发而不是参考更新答案,或者您的应用程序是针对 universal 开发的,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 2011-04-10
    相关资源
    最近更新 更多