简体   繁体   English

如何使用Alamofire从服务器获取图像(而非url或NSData)

[英]How to get Image (not url or NSData )from server using Alamofire

I am trying to get image from server using Alamofire. 我正在尝试使用Alamofire从服务器获取图像。 It is working in postman ref:attachement 它在邮递员ref:attachment中工作

Below is my code for reference: 下面是我的代码供参考:

let headers = ["Authorization" : APITOKEN,"Content_Type" : CONTENT_TYPEVAL]


Alamofire.request(urlString!,  method: .get, parameters: nil, encoding: URLEncoding.default, headers:headers ).responseJSON { (response) in
switch response.result {
case .success(let origObject):
    debugPrint(origObject)

    if let object = origObject as? Dictionary<String,AnyObject>
    {


    }

    completion(.failure(Ya3ClientError.serverUnknownError))

case .failure(let e):
    debugPrint("error:\(e.localizedDescription)")


}

Getting error "JSON could not be serialized because of error:\\nThe data couldn't be read because it isn't in the correct format." 得到错误“由于错误,JSON无法序列化:\\ n由于数据格式不正确,因此无法读取。”

Any help how to solve this issue. 任何帮助如何解决此问题。

Instead of using .responseJson , you can try using .responseData to get a Data object and create the image using UIImage(data:) 除了尝试使用.responseJson ,还可以尝试使用.responseData获取Data对象并使用UIImage(data:)创建图像

Take a look at this 看看这个

You can create a UIImage from a Data object, which you get from the response with Alamofire. 您可以从Data对象创建UIImage ,该对象是通过Alamofire从响应中获得的。 However, without seeing your JSON response I cannot help with the exact code. 但是,如果没有看到您的JSON响应,我将无法提供确切的代码。 If the response only contains the image as its data, then you can use 如果响应仅包含图像作为其数据,则可以使用

Alamofire.request(.GET, "https://myUrl/myPicture.png").response { (request, response, data, error) in
    myImageView.image = UIImage(data: data)
}

You can also have a look at the Alamofire Image library. 您也可以查看Alamofire图片库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM