AlamofireImage: unable to get image in completion block from af_setImageWithURL

everything

I am learning Swift and I am trying to set an image in a UIImageView using AlamofireImage. I am using the following code:

self.listImageView.af_setImageWithURL( NSURL(string: list!.image!)!, placeholderImage: nil, filter: nil, imageTransition: .CrossDissolve(0.5), completion:{ image in print(image) } ) 

and the result in the console is as follows:

 SUCCESS: <UIImage: 0x7fb0c3ec3d30>, {512, 286} 

My goal is to do something with the loaded image, but the problem is that I don’t understand the signature for the completion callback, and I don’t know how to access the image in the completion block. According to the documentation, Result<UIImage, NSError> .

I think it’s really easy, but I don’t understand it.

thanks

+7
ios alamofireimage
source share
1 answer

The image variable passed to the completion block is actually Alamofire.Response , not the base UIImage instance, which was implausible.

You need to update the completion block as shown below to get the actual image from the response:

 self.listImageView.af_setImageWithURL( URL(string: list!.image!)!, placeholderImage: nil, filter: nil, imageTransition: .CrossDissolve(0.5), completion: { response in print(response.result.value) # UIImage print(response.result.error) # NSError } ) 

First you can check response.result.isSuccess (or its brother response.result.isFailure ) to make sure the image was successfully extracted or not.

+21
source share

All Articles