user2931321
user2931321

Reputation: 476

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.

Upvotes: 0

Views: 835

Answers (2)

Sean
Sean

Reputation: 1534

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

Upvotes: 2

David Pasztor
David Pasztor

Reputation: 54706

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.

Upvotes: 0

Related Questions