I am trying to load image from json using Alomofire and swiftyJSON. Json is dictionary:
{"name": {"image": "https://...",}}
Alamorefire and SwiftyJSON
var imageLoad: String!
Alamofire.request(.GET, url).responseJSON { (response) -> Void in
if let value = response.result.value {
let json = JSON(value)
if let jsonDict = json.dictionary {
let image = jsonDict["name"]!["image"].stringValue
print(image) // https://....
self.imageLoad = image
}
self.tableView.reloadData()
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TVCell
// Configure the cell...
cell.imageTableView.image = UIImage(named: imageLoad) // not working "fatal error: unexpectedly found nil while unwrapping an Optional value"
If anyone can help? If there is another way feel free to write.
I share a code implemented using Swift 3 and AlamofireImage:
import UIKit
import AlamofireImage
class MovieViewCell: UICollectionViewCell {
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var overviewLabel: UILabel!
#IBOutlet weak var posterView: UIImageView!
func configure(for movie: Movie) {
titleLabel.text = movie.title
overviewLabel.text = movie.overview
let url = URL(string: movie.poster_path!)!
posterView.af_setImage(withURL: url)
}
override func prepareForReuse() {
super.prepareForReuse()
posterView.af_cancelImageRequest()
posterView.image = nil
}
}
Also, you will need Cocoapods:
platform :ios, '10.0'
inhibit_all_warnings!
use_frameworks!
target 'MovieApp' do
pod 'Alamofire', '~> 4.5'
pod 'AlamofireImage', '~> 3.3'
end
Official documentation AlamofireImage and an ImageCell.swift example
If you are using Alamofire, try AlamofireImage. But use the af_ shortcuts directly on uiimageview. Automatically you will get caching, placeholder images, and you can cancel outstanding requests if you are using a table view which could recycle the cells.
By specifying a placeholder image, the image view uses the placeholder image until the remote image is downloaded.
let imageView = UIImageView(frame: frame)
let url = URL(string: "https://httpbin.org/image/png")!
let placeholderImage = UIImage(named: "placeholder")!
imageView.af_setImage(withURL: url, placeholderImage: placeholderImage)
https://github.com/Alamofire/AlamofireImage
load image from url in tableview cell from alamofire swift3 : -
//you need install pod 'AlamofireImage', '~> 3.0’ pod
import Alamofire
import AlamofireImage
// put into cellForRowAt
Alamofire.request(self.profileImgArr[indexPath.row]).responseData { (response) in
if response.error == nil {
print(response.result)
// Show the downloaded image:
if let data = response.data {
cell.profileImg.image = UIImage(data: data)
}
}
}
I recommend using a different library for loading the images. We always do this in our projects.
There is an easy and smart library which handles pretty much everything from caching to placeholder image.
It's name is Kingfisher
https://github.com/onevcat/Kingfisher
You can use it directly on the ImageView with the URL from JSON
Example:
let url = URL(string: "url_of_your_image")
imageView.kf.setImage(with: url)
This is the most basic Asynchronous image loading method,
Take a look for yourself :)
If you're already using Alamofire, you can try AlamofireImage which is an image component library for Alamofire.
Then, fetching image is done like this:
import AlamofireImage
Alamofire.request(.GET, "https://httpbin.org/image/png")
.responseImage { response in
if let image = response.result.value {
print("image downloaded: \(image)")
}
}
As Bear with me said you can use AlamofireImage,
https://github.com/Alamofire/AlamofireImage
I made this for Swift 3, hope it can help:
In the controller implementing the tableViewDataSource
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell
NetworkService.shared.requestImage(path: path, completionHandler: {image in
cell.photo?.image = image
})
}
return cell
}
In my NetworkService (I use the shared to implement singleton, it's like getInstance) I implemented the requestImage function calling AlamofireImage:
func requestImage(path: String, completionHandler: #escaping (Image) -> Void){
Alamofire.request("\(path)").responseImage(imageScale: 1.5, inflateResponseImage: false, completionHandler: {response in
guard let image = response.result.value else{
print(response.result)
return
}
DispatchQueue.main.async {
completionHandler(image)
}
})
}
As you can see I use the GCD for main queue to manage the completionHandler just to be sure that it is the main queue which handles the view modification.
Related
This question already has answers here:
Loading/Downloading image from URL on Swift
(39 answers)
Closed 4 years ago.
still learning programming and have a question.
im trying to download image from url and put it in cells, ive successfully done it with text but not with images.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier:"searchCell", for: indexPath)
as! CustomTableViewCell
cell.titleField?.text = posts[indexPath.row].caption
cell.descriptionField?.text = posts[indexPath.row].description
cell.tagsField?.text = posts[indexPath.row].tags
let photoUrl = posts[indexPath.row].photoUrl
cell.SearchImage.image = ...
//url is stored in photoUrl
return cell
}
}
Use a pod to make it easy
use SDWebImage
then use it like this :
import SDWebImage
cell.SearchImage.sd_setImage(with: photoUrl, placeholderImage: nil)
I recommend you to use this third party library.
Kingfisher handles everything for you.
A lightweight, pure-Swift library for downloading and caching images from the web.
If you don't know how to install it, have a look at Cocoapods, it's pretty straightforward.
Anyway, here is the only line of code needed to set an image from your URL with Kingfisher :
cell.SearchImage.kf.setImage(with: photoUrl)
By the way, you should follow the Swift convention described here in order to make your code easier to read and "universal".
Have a look : Naming Convention
On swift3 You can use Alamofire (https://cocoapods.org/) for that.
Step 1:
Integrate using pods.
pod 'Alamofire', '~> 4.7'
pod 'AlamofireImage', '~> 3.3'
Step 2:
import AlamofireImage
import Alamofire
Step 3:
Alamofire.request("https:// YOUR URL").responseImage { response in
if let image = response.result.value {
print("image downloaded: \(image)")
self.myImageview.image = image
}
}
Or you can try this -
let url = URL(string: "https:// YOUR URL")
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard let data = data, error == nil else { return }
DispatchQueue.main.async()
{
self.imageView.image = UIImage(data: data)
}
}
task.resume()
Note: Please no libraries. This is important for me to learn. Also, there are a variety of answers on this but none that I found solves the issue nicely. Please don't mark as duplicate. Thanks in advance!
The problem I have is that if you scroll really fast in the table, you will see old images and flickering.
The solution from the questions I read is to cancel the URLSession
data request. But I do not know how to do that at the correct place
and time. There might be other solutions but not sure.
This is what I have so far:
Image cache class
class Cache {
static let shared = Cache()
private let cache = NSCache<NSString, UIImage>()
var task = URLSessionDataTask()
var session = URLSession.shared
func imageFor(url: URL, completionHandler: #escaping (image: Image? error: Error?) -> Void) {
if let imageInCache = self.cache.object(forKey: url.absoluteString as NSString) {
completionHandler(image: imageInCache, error: nil)
return
}
self.task = self.session.dataTask(with: url) { data, response, error in
if let error = error {
completionHandler(image: nil, error: Error)
return
}
let image = UIImage(data: data!)
self.cache.setObject(image, forKey: url.absoluteString as NSString)
completionHandler(image: image, error: nil)
}
self.task.resume()
}
}
Usage
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let myImage = images[indexPath.row]
if let imageURL = URL(string: myImage.urlString) {
photoImageView.setImage(from: imageURL)
}
return cell
}
Any thoughts?
Swift 3:
Flickering can be avoided by this way:
Use the following code in public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
cell.photoImageView.image = nil //or keep any placeholder here
cell.tag = indexPath.row
let task = URLSession.shared.dataTask(with: imageURL!) { data, response, error in
guard let data = data, error == nil else { return }
DispatchQueue.main.async() {
if cell.tag == indexPath.row{
cell.photoImageView.image = UIImage(data: data)
}
}
}
task.resume()
By checking cell.tag == indexPath.row, we are assuring that the imageview whose image we are changing, is the same row for which the image is meant to be. Hope it helps!
A couple of issues:
One possible source of flickering is that while you're updating the image asynchronously, you really want to clear the image view first, so you don't see images for prior row of reused/dequeued table view cell. Make sure to set the image view's image to nil before initiating the asynchronous image retrieval. Or, perhaps combine that with "placeholder" logic that you'll see in lots of UIImageView sync image retrieval categories.
For example:
extension UIImageView {
func setImage(from url: URL, placeholder: UIImage? = nil) {
image = placeholder // use placeholder (or if `nil`, remove any old image, before initiating asynchronous retrieval
ImageCache.shared.image(for: url) { [weak self] result in
switch result {
case .success(let image):
self?.image = image
case .failure:
break
}
}
}
}
The other issue is that if you scroll very quickly, the reused image view may have an old image retrieval request still in progress. You really should, when you call your UIImageView category's async retrieval method, you should cancel and prior request associated with that cell.
The trick here is that if you're doing this in a UIImageView extension, you can't just create new stored property to keep track of the old request. So you'd often use "associated values" to keep track of prior requests.
I am new in IOS development using Swift. I created 1 UITableView and displaying images after downloading data. But it is not smooth and some time images are displaying in wrong place when i am scrolling.
I am using AlamofireImage library for image downloading and displaying. Is there any fast library?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:HomePageCell = tableView.dequeueReusableCell(withIdentifier: "HomePage", for: indexPath) as! HomePageCell
cell.configure( homeData[0], row: indexPath, screenSize: screenSize,
hometableview: self.homeTableView);
return cell
}
import UIKit
import Alamofire
import AlamofireImage
class HomePageCell: UITableViewCell {
#IBOutlet weak var bannerImage: UIImageView!
func configure(_ homeData: HomeRequest, row: IndexPath, screenSize: CGRect, hometableview: UITableView) {
let callData = homeData.banner_lead_stories[(row as NSIndexPath).row]
let url = Constants.TEMP_IMAGE_API_URL + callData.lead_story[0].bg_image_mobile;
if( !callData.lead_story[0].bg_image_mobile.isEmpty ) {
if bannerImage?.image == nil {
let range = url.range(of: "?", options: .backwards)?.lowerBound
let u = url.substring(to: range!)
Alamofire.request(u).responseImage { response in
debugPrint(response)
//print(response.request)
// print(response.response)
// debugPrint(response.result)
if let image = response.result.value {
// print("image downloaded: \(image)")
self.bannerImage.image = image;
self.bannerImage.frame = CGRect(x: 0, y: 0, width: Int(screenSize.width), height: Int(screenSize.width/1.4))
}
}
}
} else {
self.bannerImage.image = nil;
}
}
}
It can be not smooth, because you need to cache your images and make a downloading process not in main thread(read about GCD).
For caching you can go two ways (atleast):
1) Make your own array of images where they will be cached
2) Use KingFisher for example click. It will cache your images.
For example:
yourImageView.kf.setImage(with: URL) // next time, when you will use image with this URL, it will be taken from cache.
Hope it helps
You can use SDWebImage for downloading the image array and add a placeholder image for the time being in imageView. this is function
public func sd_setImageWithURL(url: NSURL!, placeholderImage placeholder: UIImage!)
and it is as easy to use as
myImageView.sd_setImageWithURL(NSURL(string:image), placeholderImage:UIImage(named:"qwerty"))
make sure to reset you imageView in tableView delegate cellforRowAtIndexpath method by setting imageview image to nil
myImageView.image = nil
//now set image in imageView
myImageView.sd_setImageWithURL(NSURL(string:image), placeholderImage:UIImage(named:"qwerty"))
this avoids the image duplicating and weird behave of images as imageview of every cell is being reset before reusing.
Github link -> https://github.com/rs/SDWebImage
You have to use multithreading.Only UI is set in main thread, downloading image in background is in another thread.By this way you can solve your problem.
Try SDWebImage library it will save images in catch automatically and your tableView will work smoothly.
Github link -> https://github.com/rs/SDWebImage
Install pod:
platform :ios, '7.0'
pod 'SDWebImage', '~>3.8'
Just import SDWebImage like:
#import SDWebImage
And use like this:
imageView.sd_setImage(with: URL(string: "http://www.example.com/path/to/image.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
I used it in many live projects and it works like a charm :)
Use this extension to cache your images, and also don't forget to update any UI on the main thread.
let imageCache = NSCache<NSString, UIImage>()
extension UIImageView {
func loadImageUsingCacheWithURLString(_ URLString: String, placeHolder: UIImage?) {
self.image = nil
if let cachedImage = imageCache.object(forKey: NSString(string: URLString)) {
self.image = cachedImage
return
}
if let url = URL(string: URLString) {
URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
//print("RESPONSE FROM API: \(response)")
if error != nil {
print("ERROR LOADING IMAGES FROM URL: \(error)")
DispatchQueue.main.async {
self.image = placeHolder
}
return
}
DispatchQueue.main.async {
if let data = data {
if let downloadedImage = UIImage(data: data) {
imageCache.setObject(downloadedImage, forKey: NSString(string: URLString))
self.image = downloadedImage
}
}
}
}).resume()
}
}
}
I am new to iOS programming and I have a conceptual and a functional question. I tried looking at SO threads but did not get an exact question matching my situation.
I am building a simple screen where I display a list of user names along with their avatars - something like your typical Contacts screen.
I am using a UITableview for this purpose.
I first make a HTTP GET call to retrieve the list of users which returns a JSON with their names and the url to download their image. I then will store this info into Core Data and cache the images as well.
I am struggling with the part where I download the images and set it into the UIImageView.image.
Conceptually, which method should I use to get the names and the image urls - viewDidLoad or viewWillAppear? It seems to me that I should use viewWillAppear as in subsequent calls, I will be getting the list from Core Data and there is no network activity?
tableView:cellForRowAtIndexPath is the function that I use to get the image corresponding to each row. Is this correct?
Any help or pointing towards a duplicate question will help much! Thanks!
You can download all images Async process from below code...
Swift 3
private let downloadQueue = DispatchQueue(label: "me.readytoImage.downloadQueue", attributes: [])
class MainViewController: UIViewController {
fileprivate var photos = [URL]()
fileprivate var cache = NSCache<AnyObject, AnyObject>()
// MARK: - Image Downloading block
fileprivate func downloadPhoto(_ url: URL, completion: #escaping (_ url: URL, _ image: UIImage) -> Void) {
downloadQueue.async(execute: { () -> Void in
if let data = try? Data(contentsOf: url) {
if let image = UIImage(data: data) {
DispatchQueue.main.async(execute: { () -> Void in
self.cache.setObject(image, forKey: url as AnyObject)
completion(url, image)
})
}
}
})
}
Call Block
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! PhotoCell
let url = photos[indexPath.item]
//check cache images
let image = cache.object(forKey: url as AnyObject) as? UIImage
cell.imageView.backgroundColor = UIColor(white: 0.95, alpha: 1)
cell.imageView.image = image
//Downloading images
if image == nil {
downloadPhoto(url, completion: { (url, image) -> Void in
let indexPath_ = collectionView.indexPath(for: cell)
if indexPath == indexPath_ {
cell.imageView.image = image
}
})
}
return cell
}
otherwise you can also user Kingfisher SDK for download your images in Swift 3.
let url = json["image"] as? String
cell.imageProfile.kf.setImage(with: URL(string: url!))
Get the names and urls in viewDidLoad.
Make a custom UITableViewCell that takes a url for it's image. In didSet (for the url property), download the image and set the image for the UIImageView:
class CustomTableViewCell: UITableViewCell
var url: URL? = nil {
didSet {
//download and set image.
//example code can be found at the link below
}
}
}
Download image from url
I have images in my collectionViewCell's that are fetched and parsed via NSURLRequest, how do I cache these images so they don't have to start a new request with every single appearance/disappearance of the view?
here is my code that fetches the images:
class funnyPicture: NSObject {
var pfPicture : PFObject
var coverImage : UIImage!
init(pfPicture: PFObject) {
self.pfPicture = pfPicture
}
func fetchCoverImage(completion: (image: UIImage?, error: NSError?) -> Void) {
let urlString = self.pfPicture["funnyPictures"] as! String
let url = NSURL(string: urlString)
let request = NSURLRequest(URL: url!)
let queue = dispatch_get_main_queue()
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) in
if error == nil {
self.coverImage = UIImage(data: data!)
completion(image: self.coverImage, error: nil)
} else {
completion(image: nil, error: error)
}
}
}
}
and here is my collectionView code that parse the images to the collectionViewCell's:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCollectionViewCell
// Configure the cell
let book = self.books[indexPath.row]
let coverImage = book.coverImage
if coverImage == nil {
book.fetchCoverImage({ (image, error) -> Void in
if self.collectionView != nil {
collectionView.reloadItemsAtIndexPaths([indexPath])
}
})
} else {
dispatch_async(dispatch_get_main_queue()){
let imageView = cell.imageView
imageView.image = book.coverImage
}
};
if book.coverImage == nil {
cell.imageView.userInteractionEnabled = false
cell.userInteractionEnabled = false
}else {
cell.imageView.userInteractionEnabled = true
cell.userInteractionEnabled = true
}
return cell
}
While I've received references to third party frameworks, I haven't received any answer on how to implement them with the code I have provided in the question, or even an answer using apples already implemented caching mechanism.. The reason I put the code in the question was for use in an answer.. Thank you.
Here is an example for your collection view cell:
import UIKit
let imageCache = NSCache<AnyObject, AnyObject>.sharedInstance
class myCell: UICollectionViewCell {
#IBOutlet public weak var myImageView: UIImageView?
private var imageUrlString: String?
private var downloadTask: URLSessionDownloadTask?
public var imageURL: URL? {
didSet {
self.downloadItemImageForSearchResult(imageURL: imageURL)
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
public func downloadItemImageForSearchResult(imageURL: URL?) {
if let urlOfImage = imageURL {
if let cachedImage = imageCache.object(forKey: urlOfImage.absoluteString as NSString){
self.myImageView!.image = cachedImage as? UIImage
} else {
let session = URLSession.shared
self.downloadTask = session.downloadTask(
with: urlOfImage as URL, completionHandler: { [weak self] url, response, error in
if error == nil, let url = url, let data = NSData(contentsOf: url), let image = UIImage(data: data as Data) {
DispatchQueue.main.async() {
let imageToCache = image
if let strongSelf = self, let imageView = strongSelf.myImageView {
imageView.image = imageToCache
imageCache.setObject(imageToCache, forKey: urlOfImage.absoluteString as NSString , cost: 1)
}
}
} else {
//print("ERROR \(error?.localizedDescription)")
}
})
self.downloadTask!.resume()
}
}
}
override public func prepareForReuse() {
self.downloadTask?.cancel()
myImageView?.image = UIImage(named: "ImagePlaceholder")
}
deinit {
self.downloadTask?.cancel()
myImageView?.image = nil
}
}
Don't forget to make an extension for NSCache
Like this:
import Foundation
extension NSCache {
class var sharedInstance: NSCache<NSString, AnyObject> {
let cache = NSCache<NSString, AnyObject>()
return cache
}
}
Use NSCache and NSOperationQueue to manage your image loading. There's a good post outlining the technique at https://stackoverflow.com/a/12721899/5271191 (It's Objective-C, but the technique is the same for Swift.)
I highly recommend you to use a clean in place replacement/extension for UIImageView, that will manage caching of the image all transparently to you and avoid unwanted complexity of maintaining operation queues, etc.
If in memory caching suffices your needs - check this out-
https://github.com/nicklockwood/AsyncImageView
If you want persistent caching, then this one will do-
https://github.com/rs/SDWebImage
HTH.
I have images in my collectionViewCell's that are fetched and parsed
via NSURLRequest, how do I cache these images so they don't have to
start a new request with every single appearance/disappearance of the
view?
The URL loading system already provides a cache. Take a look at the docs for NSURLCache. If the resources you need aren't already being sufficiently cached, you probably only need to adjust the disk space allocated to the URL cache for your app.
You should also take a look at the headers (cache-control, expires, etc.) that come back with your resources to make sure that they're not preventing caching. Here's a short tutorial on cache-related headers.
You should use a specialized framework for that. I would not recommend using SDWebImage, it is very outdated and is not stable.
Take a look at those two libraries that are up to date with iOS platform:
DFImageManager - advanced framework written in Objective-C but featuring nullability annotations (works great with Swift). Here's a list of things that make it better, than SDWebImage. Disclosure: it's written by me, opinion might be biased.
Kingfisher - lightweight library written in Swift. Similar to SDWebImage, but has much less features that SDWebImage and DFImageManager.
I have created a library using swift 2 to do the request for image and cache it. it's very simple just give it a try.
https://github.com/georgehadly/GHImageCaching
all you can do is something like this ,
viewImg.getCachedImage("geo", URI: NSURL(string: "https://s-media-cache-ak0.pinimg.com/236x/8e/5a/98/8e5a98795dc2c5322cac97343a6cad6d.jpg")!) { (done) -> Void in
if(done){
// your extra
}
}
in case you want to delete all cached images
UIImageView.deleteAllCaching()