• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 22:16:21 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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



Best Answer-推荐答案


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

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

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap