UIImagePickerView在编辑模式下选择图像时将部分图像空白

时间:2018-07-20 06:35:11

标签: swift uiimageview uiimagepickercontroller

我正在使用UIImagePickerView来允许用户选择个人资料图像。从UIImagePickerView中选择图像后,用户选择图像的一部分(editing mode中的方形选择框),当用户选择该图像时,我会将其上载到服务器,还将其存储到该计算机的本地计算机部分当我在服务器和设备上打开该图像时,该图像会变成黑色。

这是上传后图像的外观。enter image description here

以下是我正在使用的代码。

 func launchImagePicker(){
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.allowsEditing = true
        imagePicker.navigationBar.isTranslucent = false
        imagePicker.navigationBar.barTintColor = UIColor(named:"navigationColor")
        imagePicker.navigationBar.tintColor = .white
        present(imagePicker, animated: true, completion: {
            self.closeSharedWindow()
        })
    }

以下内容将在我们取消选择器时接到电话

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

        dismiss(animated: true, completion: nil)

    }

在这里,我们正在从选择器中获取图像并上传。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        var selectedImageFromPicker: UIImage?
        if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
            selectedImageFromPicker = editedImage
        }
        else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
            selectedImageFromPicker = originalImage
        }
        if let selectedImage = selectedImageFromPicker{
            profileImageView.image = selectedImage

            self.uploadImageToServer(image: selectedImage , userId: (self.user?.userId)!) {

                self.dismiss(animated: true, completion: nil)
            }
        }
    }

以下功能会将图片上传到服务器

 func uploadImageToServer(image: UIImage,userId: String,completion: @escaping () -> ()){
        //,completion: @escaping ([String:Any])->Void
        let imgData = UIImageJPEGRepresentation(image, 0.5)!

        let parameters = ["userId": userId]

        Alamofire.upload(multipartFormData: { multipartFormData in
            multipartFormData.append(imgData, withName: "profileImage",fileName: "file.jpg", mimeType: "image/jpg")
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
        },
                         to:APPURL.updateProfileImage)
        { (result) in
            switch result {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (progress) in
                    print("Upload Progress: \(progress.fractionCompleted)")
                })

                upload.responseJSON { response in

                    self.storeProfileImageDetail(image: image)

                    guard let profileImage = UIImageJPEGRepresentation(image,0.9) else {
                        print("Error in JPG Representation Image")
                        return
                    }
                    //save image on local
                    self.saveImageToDisk(image: profileImage)
                    completion()
                }

            case .failure(_):
                self.view.makeToast("Failed to upload profile image.")
                completion()
            }
        }
    }

我无法理解任何人对此有何想法?

0 个答案:

没有答案