Alamofire way for "setImageWithURLRequest: placeholderImage: success:" in AFNetworking - ios

I'm trying to present images from url in a collection view. So far I was using AFNetworking for this, but now I'm moving to Alamofire.
I can not find a proper way to present the image like I was doing with the method 'setImageWithURLRequest: placeholderImage: success:'
Do I need to use AFNetworking in order to do this right? Is there a Alamofire way to do this that I'm skipping?
Thanks in advance
Edit
I'm currently using AlamofireImage
Here is the code I'm using:
Alamofire.request(.GET, urlString)
.responseImage { response in
if let image = response.result.value {
self.imageView.image = image
}
}
My problem is that this code is inside 'cellForItemAtIndexPath' method, and every time I scroll the images are recharged again and again. This wasn't happening with AFNetworking

You want to use the UIImageView extension in Alamofire to set the image from a URL. There is some very detailed documentation in the README.
let imageView = UIImageView(frame: frame)
let URL = NSURL(string: "https://httpbin.org/image/png")!
imageView.af_setImageWithURL(URL)
Best of luck!

Related

How to put animating placeholder image while downloading url image in swift 4 or Swift 5

I did try with Cocapods "Kingfisher" but cannot figure out how to set an animating placeholder image while the image is being downloaded.
here is the Below code I tried with kingfisher Pod
func configureCell(category:Category){
if let url = URL(string: category.imgUrl){
let placeholder = UIImage(named: "placeholder")
let options : KingfisherOptionsInfo = [KingfisherOptionsInfoItem.transition(.fade(0.1))]
categoryImg.kf.indicatorType = .activity
categoryImg.kf.setImage(with: url, placeholder: placeholder, options: options)
}
}
This is actually not an image but a UIView. The best option would be to use SkeletonView.
SkeletonView has been conceived to address this need, an elegant way to show users that something is happening and also prepare them to which contents he is waiting.
This animation is called Shimmer effect . You can use this link for your help
facebook/Shimmer.

GIF images to UIImageView using AlamofireImage in Swift

I am using AlamofireImage library to download/cache web images and show it in UIImageView inside tableViewCells.
imageView.af_setImage(withURL: url, placeholderImage: nil, filter: nil, imageTransition: .crossDissolve(0.3), runImageTransitionIfCached: true, completion: { (response) in
//...... other code .....
})
It works perfect for .png/.jpg or other still images but I am not able to show GIF images using this.
I tried using external library to convert imageData to gif images and it works perfect however Alamofire is not caching the gif data and next time the image loads as still image.
Check the code below:
imageView.af_setImage(withURL: url, placeholderImage: nil, filter: nil, imageTransition: .crossDissolve(0.3), runImageTransitionIfCached: true, completion: { (response) in
if imageUrl.hasSuffix("gif") {
if let data = response.data{
self.imageView.image = UIImage.gifImageWithData(data)
}
}
})
The above code shows the GIF for first time but next time only still image appears.
Any idea how the following can be achieved using AlamofireImage:
Download the GIF imageData for the first time, cache it and show GIF to imageView
Next time get the imageData from cache and show GIF again
I have not found a direct way to play GIF with AlamofireImage but we can do using SwiftyGif. I'm using AlamofireImage throughout the app but for display GIF and load from the server, I'm using SwiftyGif. Using SwiftyGif you can play, load from the server as well as locally. I think it will be reliable solution instead of use SDWebImage.
SwiftyGif
// You can also set it with an URL pointing to your gif
let url = URL(string: "...")
let loader = UIActivityIndicatorView(style: .white)
cell.gifImageView.setGifFromURL(url, customLoader: loader)
Download GIF file , next time get the file and show
Alamofire.download(gifUrl, to: destination).responseJSON(completionHandler: {
response in
if let filePath = response.destinationURL?.path{
if let gifImage = UIImage.init(contentsOfFile: filePath){
self.imageVIew.image = gifImage
}
}
})

Alamofireimage (Swift3) Ambiguous reference to member 'download(

Hi guys i am trying to download a bunch of images in my app using the Alamofireimage library. this is the code that i use and it is basically what they have in their documentation:
let downloader = ImageDownloader()
for (img):(ImageData) in imgDownloadList
{
let urlRequestx = img.downloadUrl
downloader.download(urlRequestx){ response in
print(response.request)
print(response.response)
debugPrint(response.result)
if let image = response.result.value {
print(image)
}
}
}
i got the error
Ambiguous reference to member
'download(_:receiptID:filter:progress:progressQueue:completion:)'
did someone face the same problem?
Thanks a lot.

Getting Memory waning in UIImage?(data:)

I am getting an Image as Data from server.To show it I am using UIImage?(data:) to show it.For a specific image application is getting crash because of memory pressure. What could be the reason, how to fix it.
if let imgData = Utils.fetchDataFromDocumentDirectory(imageName:"test.jpg"){
attachmentImgView.imageĀ = UIImage(data:imgData)
}
How big is the Image? and are you downloading only 1 image or an array?
This might be caused from the size of the image you are trying to download.
My advice if you are trying to show an image, only giving it the url like so:
let url = URL(string: "http://i.imgur.com/w5rkSIj.jpg")
let data = try? Data(contentsOf: url)
if let imageData = data {
let image = UIImage(data: data)
}
even better you can try to use AsyncImageView 3rd party library where you give it the url of the image and it shows it asynchronously without delays in the app.
Hope this helps!

AlamofireImage Usage

I'm trying to understand the way to use [AlamofireImage][1]
i have a few questions:
when i use:
Alamofire.request("https://httpbin.org/image/png").responseImage { response in
if let image = response.result.value {
//Do Somthing
}
}
is that all i need to do to use swift native URLCache?
Does it caching it automatically?
after the first request above, can i use AlamofiremImage fetch method?
// Fetch
let cachedImage = imageCache.image(withIdentifier: "https://httpbin.org/image/png")
or do i have to use URLCache cachedResponse(for: URLRequest) method?
(if the answer to 2 is "no") Do i need to actively save the image cache with alamfireImage Add method after the request response?
// Add
imageCache.add(image, withIdentifier: "https://httpbin.org/image/png")
(with relation to question 1) isn't it creating additional cache to the same image and url (once with URLCache and another with AlamofireImage add method)?
thanks!

Resources