ImageStore is image downloader with memory cache supporting.
To run the example project, clone the repo, and run pod install
from the Example directory first.
- Swift3.2
- iOS10.0 or higher
- cocoapods 1.4.0 or higher
ImageStore is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "ImageStore"
ImageStore's config struct.
-
maxDownloadSize
Max download size at one image. (default 2MB) -
cacheLimit
Cache size. (default 200MB)
init(maxDownloadSize: Int64, cacheLimit: Int64)
Image downloader class.
-
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.
-
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 extension for using ImageStore.
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.
miup, [email protected]
ImageStore is available under the MIT license. See the LICENSE file for more info.
- 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.