图像视图中显示了一些随机图像,而不是URL中的实际图像:iOS,Swift

时间:2019-02-22 08:00:45

标签: ios swift iphone

我已经实现了更改个人资料图片的功能。实施流程如下

  

选择图像->上载到服务器->从服务器下载并显示   它的用户。

此流程随机运行正常。图片上传总是成功的,而我总是会得到一个URL作为响应。但是,当我尝试显示该图像时,以某种方式无法在图像视图中显示该图像。但相同的URL在所有浏览器(Sfari,chrome,IE)中显示图像。我无法理解上载或下载图像时遇到的问题。

代码附在下面。任何帮助,将不胜感激。

  

图片上传:-

func updateProfile(userUpdateRequest:UsersRequest ,completionBlock completion: @escaping ( _ fileURL : String)->Void, failureBlock failure: @escaping ( _ error: String) -> Void)  {

        let url = "\(SPConfigurations.getBaseURL())/\(APIConstants.getProfileUpdateRequestRoute(userRequest: userUpdateRequest))"

        Alamofire.upload(multipartFormData: { multipartFormData in
            if (userUpdateRequest.image != nil) {
                let  fileData = userUpdateRequest.image!.jpegData(compressionQuality: 0.5)!
                 multipartFormData.append(fileData, withName: "file", fileName:UUID().uuidString , mimeType: "image/jpeg")
            }
        }, to: url, method: .post, headers:APIConstants.authHeaders()) { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in

                    if let err = response.error {
                        failure(err.localizedDescription)
                        return
                    } else {
                        if let result = response.result.value as? [String:Any] {
                            if let dataValue = result["data"] as? [String:Any] {
                                if let fileUrl = dataValue["profile_picture_url"] as? String {
                                    completion(fileUrl)
                                } else {
                                    completion("")
                                }
                            }
                        }
                    }
                }
            case .failure(let encodingError):
                failure(encodingError.localizedDescription)
            }
        }
    }
  

图片下载:-Imageview的扩展及其工作正常   除了我要上传的图像以外的所有图像。

func setImage (urlString: String,placeHolderString : String = "placeholder") {

        let placeHolderImage = UIImage.init(named: placeHolderString)

        if(urlString.trim() == "") {
            self.image = placeHolderImage
            return
        }

        self.kf.setImage(with: URL(string: urlString)!, placeholder: placeHolderImage, options: nil, progressBlock: { (receivedSize, totalSize) -> () in

        }, completionHandler: { (image, error, cacheType, imageURL) -> () in
            DispatchQueue.main.async {
                if(image != nil) {
                    self.image = image
                }
            }
        }
        )
    }
  

未显示的示例URL:   https://snikpic-image-bucket.s3.us-east-2.amazonaws.com/5013571a-f020-4321-a59f-522da6269635/d7872db2-5b9f-4d4c-8839-7604bffc02d7.jpeg

1 个答案:

答案 0 :(得分:0)

尝试设置没有进度条的图像

self.imageView?.kf.setImage(with: URL(string: urlString)!)
相关问题