How to upload a user profile image [closed]

夙愿已清 提交于 2020-01-25 06:49:06

问题


I am trying to upload a profile image to the user profile but the profile image doesn't appear on the profile page there are no errors popping up so how do I restructure this code to fix this issue?

 if let ProfileImageUrl = dictionary?["photo"] as? String {
                     let url = URL(string: ProfileImageUrl)
                    URLSession.shared.dataTask(with: url!, completionHandler: { ( data, response, Error ) in
                        if Error != nil {
                            print(Error!)
                        return
            }
                        DispatchQueue.main.async {
                            self.ProfileImage.image = UIImage(data: data!)
                        }

回答1:


The "Error" in trailing closure should be "error". It's just a matter of naming convention, doesn't necessarily cause the code to fail.

Found the problem, you're missing ".resume()" on the data task. The following code works :

if let imageUrl = urlString{
        let url = URL(string: imageUrl)
        URLSession.shared.dataTask(with: url!) { (data, response, error) in
            if let error = error{
                print("Error : \(error)")
                return
            }
            DispatchQueue.main.async {
                self.image.image = UIImage(data: data!)
            }
        }.resume()

    }



回答2:


Firstly You will need to uploaded by button (browser uploaded ) so just create button and then create button action

@IBAction func doUplodPImg(_ sender: Any) {
    let image = UIImagePickerController()
    image.delegate = self

    image.sourceType = UIImagePickerController.SourceType.photoLibrary

    image.allowsEditing = false

    self.present(image, animated: true)
    {
        //After it is complete
    }

}

and also add one function

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
{
    if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
            {
                profileImg.image = image
            }
            else
            {
                //Error message
            }
    guard let selectedImage = info[.originalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }
    profileImg.image = selectedImage

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

Now you can successfully uploaded photo in UIimageview. please change your own name profileImg to your UIimageview name

make sure you added UIImagePickerControllerDelegate in your top viewcontroller delegate

and If you want to uploaded in server then just use alamofire method

    Alamofire.upload(
        multipartFormData: { multipartFormData in
            if let _image = self.profileImg.image {
                if let imageData = _image.jpegData(compressionQuality: 0.7) {
                    multipartFormData.append(imageData, withName: "profile_image", fileName: "image.jpg", mimeType: "image/jpg")
                }
            }
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
    }, to: url, method: .post)
    { (response) in
        switch response {
        case .success(let upload,_,_):
            upload.responseJSON { res in
                let response = res.result.value as? NSDictionary
                print(response)
  }
        case .failure(_): break

        }

}

change your parameters name im using this profile_image (server side parameter)

Hope it will be helpful. let me know if you find any difficulty.



来源:https://stackoverflow.com/questions/59816638/how-to-upload-a-user-profile-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!