Explorar o código

Add a fallback context creation for strange inputs

onevcat hai 1 ano
pai
achega
2e24a6df41
Modificáronse 1 ficheiros con 19 adicións e 5 borrados
  1. 19 5
      Sources/Image/ImageDrawing.swift

+ 19 - 5
Sources/Image/ImageDrawing.swift

@@ -355,13 +355,13 @@ extension KingfisherWrapper where Base: KFCrossPlatformImage {
         }
         }
         guard let outContext = CGContext(
         guard let outContext = CGContext(
             data: outBuffer.data,
             data: outBuffer.data,
-            width: Int(outBuffer.width),
-            height: Int(outBuffer.height),
-            bitsPerComponent: 8,
-            bytesPerRow: outBuffer.rowBytes,
+            width: cgImage.width,
+            height: cgImage.height,
+            bitsPerComponent: cgImage.bitsPerComponent,
+            bytesPerRow: cgImage.bytesPerRow,
             space: colorSpace,
             space: colorSpace,
             bitmapInfo: cgImage.bitmapInfo.rawValue
             bitmapInfo: cgImage.bitmapInfo.rawValue
-        ) else {
+        ) ?? .fallback(data: outBuffer.data, cgImage: cgImage) else {
             assertionFailure("[Kingfisher] Creating CG context failed.")
             assertionFailure("[Kingfisher] Creating CG context failed.")
             return base
             return base
         }
         }
@@ -693,3 +693,17 @@ extension KingfisherWrapper where Base: KFCrossPlatformImage {
     }
     }
     #endif
     #endif
 }
 }
+
+extension CGContext {
+    fileprivate static func fallback(data: UnsafeMutableRawPointer?, cgImage: CGImage) -> CGContext? {
+        return CGContext(
+            data: data,
+            width: cgImage.width,
+            height: cgImage.height,
+            bitsPerComponent: 8,
+            bytesPerRow: 4 * cgImage.width,
+            space: CGColorSpaceCreateDeviceRGB(),
+            bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
+        )
+    }
+}