Просмотр исходного кода

Minor update to omit of optional source

onevcat 7 лет назад
Родитель
Сommit
464aad3e6a

+ 22 - 21
README.md

@@ -13,21 +13,22 @@
 <a href="#sponsors" alt="Sponsors on Open Collective"><img src="https://opencollective.com/Kingfisher/sponsors/badge.svg" /></a>
 </p>
 
-Kingfisher is a lightweight, pure-Swift library for downloading and caching images from the web. This project is heavily inspired by the popular [SDWebImage](https://github.com/rs/SDWebImage). It provides you a chance to use a pure-Swift alternative in your next app.
+Kingfisher is a powerful, pure-Swift library for downloading and caching images from the web. It provides you a chance to use a pure-Swift alternative in your next app.
 
 ## Features
 
 - [x] Asynchronous image downloading and caching.
-- [x] `URLSession`-based networking. Basic image processors and filters supplied.
+- [x] Loading image from either `URLSession`-based networking or local provided data.
+- [x] Useful image processors and filters provided.
 - [x] Multiple-layer hybrid cache for both memory and disk.
 - [x] Fine control on cache behavior. Customizable expiration date and size limit.
-- [x] Cancelable downloading and processing tasks to improve performance.
+- [x] Cancelable downloading and auto-reusing previous downloaded content to improve performance.
 - [x] Independent components. Use the downloader, caching system and image processors separately as you need.
-- [x] Prefetching images and showing them from cache later when necessary.
-- [x] Extensions for `UIImageView`, `NSImage` and `UIButton` to directly set an image from a URL.
+- [x] Prefetching images and showing them from cache to boost your app.
+- [x] View extensions for `UIImageView`, `NSImage`, `NSButton` and `UIButton` to directly set an image from a URL.
 - [x] Built-in transition animation when setting images.
 - [x] Customizable placeholder and indicator while loading images.
-- [x] Extensible image processing and image format supported.
+- [x] Extensible image processing and image format easily.
 
 ### Kingfisher 101
 
@@ -38,30 +39,28 @@ let url = URL(string: "https://example.com/image.png")
 imageView.kf.setImage(with: url)
 ```
 
-Kingfisher will download the image from `url`, send it to both the memory cache and the disk cache, and display it in `imageView`. When you use the same code later, the image will be retrieved from cache and shown immediately.
+Kingfisher will download the image from `url`, send it to both memory cache and disk cache, and display it in `imageView`. When you set with the same URL later, the image will be retrieved from cache and shown immediately.
 
-### More Interesting Example
+### A More Advanced Example
 
-With the options and callbacks, you can express hard tasks and use Kingfisher in a powerful way. For example, the code below: 
+With the powerful options, you can do hard tasks with Kingfisher in a simple way. For example, the code below: 
 
-1. Downsamples a high resolution image to match the image view size.
-2. Makes it round cornered with a given radius.
-3. Shows a system indicator and a placeholder image while downloading.
-4. When prepared, it animates the small thumbnail image with a fade in effect. 
-5. The the original large image is also cached to disk for later use, to get rid of downloading again in a detail view.
-6. A console log is printed when the task finishes, either for success or failure.
+1. Downloads a high resolution image.
+2. Downsamples it to match the image view size.
+3. Makes it round cornered with a given radius.
+4. Shows a system indicator and a placeholder image while downloading.
+5. When prepared, it animates the small thumbnail image with a "fade in" effect. 
+6. The the original large image is also cached to disk for later use, to get rid of downloading it again in a detail view.
+7. A console log is printed when the task finishes, either for success or failure.
 
 ```swift
 let url = URL(string: "https://example.com/high_resolution_image.png")
-let scale = UIScreen.main.scale
-let placeholderImage = UIImage(named: "placeholderImage")
 let processor = DownsamplingImageProcessor(size: imageView.size)
              >> RoundCornerImageProcessor(cornerRadius: 20)
-
 imageView.kf.indicatorType = .activity
 imageView.kf.setImage(
     with: resource,
-    placeholder: placeholderImage,
+    placeholder: UIImage(named: "placeholderImage"),
     options: [
         .processor(processor),
         .scaleFactor(UIScreen.main.scale),
@@ -79,9 +78,11 @@ imageView.kf.setImage(
 }
 ```
 
+It is really a very common situation I will meet in my daily work. Think about how many lines you need to write if without Kingfisher. You will fall in love with it!
+
 ### Learn More
 
-To learn the using Kingfisher by more examples, take a look at the [Cheat Sheet](https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet). There we summarized most common tasks in Kingfisher, you can get a better idea on what this framework can do.
+To learn the using Kingfisher by more examples, take a look at the [Cheat Sheet](https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet). There we summarized most common tasks in Kingfisher, you can get a better idea on what this framework can do. There are also some tips for performance in the same page, remember to check them too.
 
 ## Requirements
 
@@ -129,7 +130,7 @@ This project exists thanks to all the people who contribute. [[Contribute]](http
 
 ## Backers
 
-Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/kingfisher#backer)]
+Thank you to all our backers! Your support is really important for the project and encouraging us to continue. 🙏 [[Become a backer](https://opencollective.com/kingfisher#backer)]
 
 <a href="https://opencollective.com/kingfisher#backers" target="_blank"><img src="https://opencollective.com/kingfisher/backers.svg?width=890"></a>
 

+ 1 - 1
Sources/Cache/DiskStorage.swift

@@ -277,7 +277,7 @@ public enum DiskStorage {
             return removed
         }
 
-        // Get the total file size of the folder in bytes.
+        /// Get the total file size of the folder in bytes.
         func totalSize() throws -> UInt {
             let propertyKeys: [URLResourceKey] = [.fileSizeKey]
             let urls = try allFileURLs(for: propertyKeys)

+ 9 - 9
Sources/Cache/ImageCache.swift

@@ -166,18 +166,13 @@ open class ImageCache {
     /// - Parameters:
     ///   - memoryStorage: The `MemoryStorage.Backend` object to use in the image cache.
     ///   - diskStorage: The `DiskStorage.Backend` object to use in the image cache.
-    ///   - name: A name used as a part of the bound IO queue.
     public init(
         memoryStorage: MemoryStorage.Backend<Image>,
-        diskStorage: DiskStorage.Backend<Data>,
-        name: String = "")
+        diskStorage: DiskStorage.Backend<Data>)
     {
         self.memoryStorage = memoryStorage
         self.diskStorage = diskStorage
-        var ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue"
-        if !name.isEmpty {
-            ioQueueName.append(".\(name)")
-        }
+        let ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue.\(UUID().uuidString)"
         ioQueue = DispatchQueue(label: ioQueueName)
         
         #if !os(macOS) && !os(watchOS)
@@ -248,7 +243,7 @@ open class ImageCache {
         let diskStorage = try DiskStorage.Backend<Data>(config: diskConfig)
         diskConfig.cachePathBlock = nil
         
-        self.init(memoryStorage: memoryStorage, diskStorage: diskStorage, name: name)
+        self.init(memoryStorage: memoryStorage, diskStorage: diskStorage)
     }
     
     deinit {
@@ -598,6 +593,11 @@ open class ImageCache {
             }
         }
     }
+
+    /// Clears the expired images from disk storage. This is an async operation.
+    open func cleanExpiredMemoryCache() {
+        memoryStorage.removeExpired()
+    }
     
     /// Clears the expired images from disk storage. This is an async operation.
     @objc func cleanExpiredDiskCache() {
@@ -757,7 +757,7 @@ open class ImageCache {
     /// This method does not guarantee there is an image already cached in the returned path. It just gives your
     /// the path that the image should be, if it exists in disk storage.
     ///
-    /// You could use `isImageCached(forKey:)` method to check whether the image is cached under that key in disk.
+    /// You could use `isCached(forKey:)` method to check whether the image is cached under that key in disk.
     open func cachePath(
         forKey key: String,
         processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> String

+ 5 - 5
Sources/General/Deprecated.swift

@@ -98,7 +98,7 @@ extension KingfisherManager {
         return retrieveImage(with: resource, options: options, progressBlock: progressBlock) {
             result in
             switch result {
-            case .success(let value): completionHandler?(value.image, nil, value.cacheType, value.source?.url)
+            case .success(let value): completionHandler?(value.image, nil, value.cacheType, value.source.url)
             case .failure(let error): completionHandler?(nil, error as NSError, .none, resource.downloadURL)
             }
         }
@@ -149,7 +149,7 @@ extension KingfisherWrapper where Base: ImageView {
             result in
             switch result {
             case .success(let value):
-                completionHandler?(value.image, nil, value.cacheType, value.source?.url)
+                completionHandler?(value.image, nil, value.cacheType, value.source.url)
             case .failure(let error):
                 completionHandler?(nil, error as NSError, .none, nil)
             }
@@ -180,7 +180,7 @@ extension KingfisherWrapper where Base: UIButton {
             result in
             switch result {
             case .success(let value):
-                completionHandler?(value.image, nil, value.cacheType, value.source?.url)
+                completionHandler?(value.image, nil, value.cacheType, value.source.url)
             case .failure(let error):
                 completionHandler?(nil, error as NSError, .none, nil)
             }
@@ -207,7 +207,7 @@ extension KingfisherWrapper where Base: UIButton {
             result in
             switch result {
             case .success(let value):
-                completionHandler?(value.image, nil, value.cacheType, value.source?.url)
+                completionHandler?(value.image, nil, value.cacheType, value.source.url)
             case .failure(let error):
                 completionHandler?(nil, error as NSError, .none, nil)
             }
@@ -236,7 +236,7 @@ extension KingfisherWrapper where Base: WKInterfaceImage {
             result in
             switch result {
             case .success(let value):
-                completionHandler?(value.image, nil, value.cacheType, value.source?.url)
+                completionHandler?(value.image, nil, value.cacheType, value.source.url)
             case .failure(let error):
                 completionHandler?(nil, error as NSError, .none, nil)
             }

+ 6 - 1
Sources/General/ImageSource/Source.swift

@@ -55,13 +55,18 @@ public enum Source {
     /// from local storage or in any other encoding format (like Base64).
     case provider(ImageDataProvider)
     
+    /// The cache key defined for this source value.
     public var cacheKey: String {
         switch self {
         case .network(let resource): return resource.cacheKey
         case .provider(let provider): return provider.cacheKey
         }
     }
-    
+
+    /// The URL defined for this source value.
+    ///
+    /// For a `.network` source, it is the `downloadURL` of associated `Resource` instance.
+    /// For a `.provider` value, it is always `nil`.
     public var url: URL? {
         switch self {
         case .network(let resource): return resource.downloadURL

+ 1 - 1
Sources/General/KingfisherManager.swift

@@ -44,7 +44,7 @@ public struct RetrieveImageResult {
     public let cacheType: CacheType
 
     /// The `Source` from which the retrieve task begins.
-    public let source: Source?
+    public let source: Source
 }
 
 /// Main manager class of Kingfisher. It connects Kingfisher downloader and cache,

+ 1 - 1
Tests/KingfisherTests/ImageViewExtensionTests.swift

@@ -378,7 +378,7 @@ class ImageViewExtensionTests: XCTestCase {
         group.enter()
         self.imageView.kf.setImage(with: testURLs[1]) { result in
             XCTAssertNotNil(result.value?.image)
-            XCTAssertEqual(result.value?.source?.url, testURLs[1])
+            XCTAssertEqual(result.value?.source.url, testURLs[1])
             XCTAssertEqual(result.value!.image, self.imageView.image)
             group.leave()
         }