iOS如何从相机加载照片来解析Swift

时间:2018-04-13 05:31:54

标签: ios swift parse-platform

如何从相机或手机库中加载照片进行解析,例如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
            }
        })
    }

}

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

1 个答案:

答案 0 :(得分:3)

试试这个。 点击按钮调用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)
}