简体   繁体   中英

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

I am trying to get image from server using Alamofire. It is working in postman ref:attachement

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."

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

Take a look at this

You can create a UIImage from a Data object, which you get from the response with Alamofire. However, without seeing your JSON response I cannot help with the exact code. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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