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

1. Convert data directly to [UInt8] to avoid memory copy.
2. Modify the wrong code comment

fuyoufang 4 лет назад
Родитель
Сommit
78b2d02e19
2 измененных файлов с 8 добавлено и 9 удалено
  1. 3 3
      Sources/Cache/ImageCache.swift
  2. 5 6
      Sources/Image/ImageFormat.swift

+ 3 - 3
Sources/Cache/ImageCache.swift

@@ -474,6 +474,8 @@ open class ImageCache {
         }
         }
     }
     }
 
 
+    // MARK: Getting Images
+
     /// Gets an image for a given key from the cache, either from memory storage or disk storage.
     /// Gets an image for a given key from the cache, either from memory storage or disk storage.
     ///
     ///
     /// - Parameters:
     /// - Parameters:
@@ -533,8 +535,6 @@ open class ImageCache {
         }
         }
     }
     }
 
 
-    // MARK: Getting Images
-
     /// Gets an image for a given key from the cache, either from memory storage or disk storage.
     /// Gets an image for a given key from the cache, either from memory storage or disk storage.
     ///
     ///
     /// - Parameters:
     /// - Parameters:
@@ -564,7 +564,7 @@ open class ImageCache {
     ///
     ///
     /// - Parameters:
     /// - Parameters:
     ///   - key: The key used for caching the image.
     ///   - key: The key used for caching the image.
-    ///   - options: The `KingfisherParsedOptionsInfoKingfisherParsedOptionsInfo` options setting used for retrieving the image.
+    ///   - options: The `KingfisherParsedOptionsInfo` options setting used for retrieving the image.
     /// - Returns: The image stored in memory cache, if exists and valid. Otherwise, if the image does not exist or
     /// - Returns: The image stored in memory cache, if exists and valid. Otherwise, if the image does not exist or
     ///            has already expired, `nil` is returned.
     ///            has already expired, `nil` is returned.
     open func retrieveImageInMemoryCache(
     open func retrieveImageInMemoryCache(

+ 5 - 6
Sources/Image/ImageFormat.swift

@@ -115,13 +115,12 @@ extension KingfisherWrapper where Base == Data {
             return false
             return false
         }
         }
         
         
-        var buffer = [UInt8](repeating: 0, count: base.count)
-        base.copyBytes(to: &buffer, count: base.count)
-        for (index, item) in buffer.enumerated() {
+        let bytes = [UInt8](base)
+        let markerBytes = marker.bytes
+        for (index, item) in bytes.enumerated() where bytes.count > index + 1 {
             guard
             guard
-                item == marker.bytes.first,
-                buffer.count > index + 1,
-                buffer[index + 1] == marker.bytes[1] else {
+                item == markerBytes.first,
+                bytes[index + 1] == markerBytes[1] else {
                 continue
                 continue
             }
             }
             return true
             return true