Reputation:
I'm new in the Swift, I am using Alamofire
framework in my project, I am facing the following issue while giving the request for download image, I am using following code:
let imageURL = Constant.BaseAPI_URL + "/" + driver.driverProfile
Alamofire.request(.GET, imageURL).response() {
(_, _, data, _) in
let image = UIImage(data: data! as! NSData)
deiverProfileImageView.image = image
}
Issue :
Click Here For Image Description
Upvotes: 1
Views: 3072
Reputation: 450
Try this:
let imageUrl = "www.your_url.com"
Alamofire.download(imageUrl).responseData {
response in
if let data = response.result.value {
let image = UIImage(data: data)
ProfileImage.image = image
}
}
or
The easiest way according to me will be using SDWebImage
Add this to your pod file
pod 'SDWebImage', '~> 4.0'
Run pod install
Now import SDWebImage
import SDWebImage
Now for setting image from url
imageView.sd_setImage(with: URL(string: "http://www.domain/path/to/image.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
It will show placeholder image but when image is downloaded it will show the image from url .Your app will never crash
This are the main feature of SDWebImage
Categories for UIImageView, UIButton, MKAnnotationView adding web image and cache management
An asynchronous image downloader
An asynchronous memory + disk image caching with automatic cache expiration handling
A background image decompression
A guarantee that the same URL won't be downloaded several times
A guarantee that bogus URLs won't be retried again and again
A guarantee that main thread will never be blocked Performances!
Use GCD and ARC
To know more https://github.com/rs/SDWebImage
Upvotes: 0
Reputation: 5666
Try below code
let imageURL = Constant.BaseAPI_URL + "/" + driver.driverProfile
Alamofire.request(imageURL).responseData(completionHandler: { response in
if let imageData = response.data
{
deiverProfileImageView.image = imageData
}
})
Upvotes: 0
Reputation: 3301
Try this:
let imageURL = Constant.BaseAPI_URL + "/" + driver.driverProfile
Alamofire.download(imageURL).responseData { response in
if let data = response.result.value {
let image = UIImage(data: data)
deiverProfileImageView.image = image
}
}
Upvotes: 2