ImageStore
ImageStore is image downloader with memory cache supporting.
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Requirements
- Swift3.2
- iOS10.0 or higher
- cocoapods 1.4.0 or higher
Installation
ImageStore is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "ImageStore"
Classes, Structs, extensions
ImageStoreConfig
ImageStore's config struct.
property
-
maxDownloadSize
Max download size at one image. (default 2MB) -
cacheLimit
Cache size. (default 200MB)
methods
init(maxDownloadSize: Int64, cacheLimit: Int64)
ImageStore
Image downloader class.
property
-
shared: ImageStore
(static)
Singleton shared instance. -
config: ImageStoreConfig
ImageStore's config. -
completionsByURLString: [String: [ImageStoreCompletionHandler]]
Completion handlers dictionary indexed by source URL String. -
downloadTaskByURLString: [String: URLSessionDownloadTask]
Download task dictionary indexed by source URL String. -
cache: NSCache<AnyObject, UIImage>
Image cache. -
queue: OperationQueue
Image download queue. -
session: URLSession
Image download URLSesson.
methods
-
reset(config: ImageStoreConfig)
(class)
Create new shared instance from ImageStoreConfig. -
load(_ url: URL, completion: ImageStoreCompletionHandler?)
Download image from URL, and execute CompletionHandler. If cached, only execute CompletionHandler.
If download task has suspended, resume it.
let url = URL(string: "your.images.com/0.jpg")
ImageStore.shared.load(url) { [weak self] image in
self?.myImageView.image = image
}
ImageStore.shared.load(url) { [weak self] image in
self?.mySecondImageView.image = image
}
If you write like this, only one download task is create. But two completion handlers are executed.
-
suspendIfResuming(url: URL)
If ImageStore has download task with argument URL and it resuming, suspend it.
You can resume itload(url)
function. -
cancel(url: URL)
Remove download task with argument URL.
UIImageView+ImageStore
UIImageView extension for using ImageStore.
methods
load(_ url: URL, placeholderImage: UIImage?, shouldSetImageConditionBlock: @escaping (() -> Bool))
Second argumentshoulSetImageConditionBlock
is closure that returns a condition allow ImageView to display image.
It needs for resusable view (e.g. UITableViewCell, UICollectionViewCell).
When reused thats views, you may change iamge of cell's ImageView.
You must callsuspendIfResuming(url: URL)
orcancel(url: URL)
at cell'sprepareForReuse
function.
ButsuspendIfResuming(url: URL)
andcancel(url: URL)
does not delete completion handler for resume or next downloading.
So if executed completion handler between afterprepareForReuse
and next download image completion handler, previous image has displayed till call next download is end.
Look follow example.
class MyTableViewCell: UITableViewCell {
static let cellIdentifier = "MyTableViewCell"
var id: String?
var myImageView: UIImageView = UIImageView()
override func prepareForReuse() {
super.prepareForReuse()
id = nil
myImageView.image = nil
myImageView.suspendLoading()
}
}
class MyListViewController: UIViewController, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: MyTableViewCell.cellIdentifier, for: indexPath) as? MyViewCell else { return UITableViewCell() }
let url = urls[indexPath.row]
let urlString = url.absoluteString
cell.id = urlString
cell.myImageView.load(url) {
// display image if cell's id and loading image url string is match.
return cell.id == urlString
}
return cell
}
}
-
cancelLoading()
Cancel current url loading. -
suspendLoading()
Suspend current url loading.
Author
miup, [email protected]
License
ImageStore is available under the MIT license. See the LICENSE file for more info.
Contribution
- If you found a bug, please open an issue.
- I'm wating for your feature request, pelase open an issue.
- I'm wating for your contribution, please create a new pull request.