ios - ImageView : iOS, Swift 中显示一些随机图像而不是来自 URL 的实际图像

标签 ios swift iphone

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

Select image -> upload to server -> Download from server and display it user.

此流程随机运行良好。图片上传总是成功,我总是得到一个 URL 作为响应。但是当我尝试显示该图像时,不知何故它没有显示在 ImageView 中。但是相同的 URL 在所有浏览器(Sfari、chrome、IE)中显示图像。我无法理解我在上传或下载图像时遇到的问题。

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

Image uploading:-

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)
            }
        }
    }

Image Downloading:- Its extension of Imageview & its working fine for all images except the ones i am uploading.

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
                }
            }
        }
        )
    }

Example URL which does not get displayed : https://snikpic-image-bucket.s3.us-east-2.amazonaws.com/5013571a-f020-4321-a59f-522da6269635/d7872db2-5b9f-4d4c-8839-7604bffc02d7.jpeg

最佳答案

尝试设置无进度条的图片

self.imageView?.kf.setImage(with: URL(string: urlString)!)

关于ios - ImageView : iOS, Swift 中显示一些随机图像而不是来自 URL 的实际图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54822523/

相关文章:

ios - 获取哪些 iOS 框架可用?

ios - 如何在 iOS 中的图像上应用 CIPhotoEffectMono 滤镜?

ios - Flurry 分析 "Table Failed to Load"错误

ios - 使用 getdatainbackground 从 Parse 检索多个图像(IOS - Swift)

swift - 无法从 iOS 应用程序打开设置 URL 字符串

iphone - 如果应用程序从 pass 开始,则获取通知

iphone - 从哪里开始制作将 MP3 文件转换为 iphone 有声读物 (M4B) 格式的程序?

objective-c - 帮助 Xcode - 类 'secondview' 的错误实现 + 找不到 '-switchview:' 的方法定义

iphone - 计算 NSArray 中 NSDictionary 中的值

ios - swift中静态函数和单例类的区别