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

Posted

技术标签:

【中文标题】iOS 如何从相机加载照片以解析 Swift【英文标题】:iOS How to load photo from camera to parse 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
            
        )
    


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

【问题讨论】:

【参考方案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 开发的,请告诉我。

以上是关于iOS 如何从相机加载照片以解析 Swift的主要内容,如果未能解决你的问题,请参考以下文章

Swift fetchAssetsInAssetCollection:从相机胶卷范围内加载照片

以纵向模式从相机拍摄的加载到画布上的照片是横向的

如何仅在 Swift 中访问从相机拍摄的图像,就像 iOS 中的画廊一样?

IOS如何以全分辨率从相机胶卷中选择图像

iOS如何获取下一行解析Swift

iOS,Swift:如何保存包含自定义对象的数组以便我可以加载以供重用?