ImageDrawing.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. //
  2. // ImageDrawing.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/09/28.
  6. //
  7. // Copyright (c) 2018 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Accelerate
  27. #if canImport(AppKit)
  28. import AppKit
  29. #endif
  30. #if canImport(UIKit)
  31. import UIKit
  32. #endif
  33. // MARK: - Image Transforming
  34. extension KingfisherWrapper where Base: Image {
  35. // MARK: Blend Mode
  36. /// Create image from `base` image and apply blend mode.
  37. ///
  38. /// - parameter blendMode: The blend mode of creating image.
  39. /// - parameter alpha: The alpha should be used for image.
  40. /// - parameter backgroundColor: The background color for the output image.
  41. ///
  42. /// - returns: An image with blend mode applied.
  43. ///
  44. /// - Note: This method only works for CG-based image.
  45. #if !os(macOS)
  46. public func image(withBlendMode blendMode: CGBlendMode,
  47. alpha: CGFloat = 1.0,
  48. backgroundColor: Color? = nil) -> Image
  49. {
  50. guard let _ = cgImage else {
  51. assertionFailure("[Kingfisher] Blend mode image only works for CG-based image.")
  52. return base
  53. }
  54. let rect = CGRect(origin: .zero, size: size)
  55. return draw(to: rect.size) { _ in
  56. if let backgroundColor = backgroundColor {
  57. backgroundColor.setFill()
  58. UIRectFill(rect)
  59. }
  60. base.draw(in: rect, blendMode: blendMode, alpha: alpha)
  61. }
  62. }
  63. #endif
  64. #if os(macOS)
  65. // MARK: Compositing
  66. /// Creates image from `base` image and apply compositing operation.
  67. ///
  68. /// - Parameters:
  69. /// - compositingOperation: The compositing operation of creating image.
  70. /// - alpha: The alpha should be used for image.
  71. /// - backgroundColor: The background color for the output image.
  72. /// - Returns: An image with compositing operation applied.
  73. ///
  74. /// - Note: This method only works for CG-based image. For any non-CG-based image, `base` itself is returned.
  75. public func image(withCompositingOperation compositingOperation: NSCompositingOperation,
  76. alpha: CGFloat = 1.0,
  77. backgroundColor: Color? = nil) -> Image
  78. {
  79. guard let _ = cgImage else {
  80. assertionFailure("[Kingfisher] Compositing Operation image only works for CG-based image.")
  81. return base
  82. }
  83. let rect = CGRect(origin: .zero, size: size)
  84. return draw(to: rect.size) { _ in
  85. if let backgroundColor = backgroundColor {
  86. backgroundColor.setFill()
  87. rect.fill()
  88. }
  89. base.draw(in: rect, from: .zero, operation: compositingOperation, fraction: alpha)
  90. }
  91. }
  92. #endif
  93. // MARK: Round Corner
  94. /// Creates a round corner image from on `base` image.
  95. ///
  96. /// - Parameters:
  97. /// - radius: The round corner radius of creating image.
  98. /// - size: The target size of creating image.
  99. /// - corners: The target corners which will be applied rounding.
  100. /// - backgroundColor: The background color for the output image
  101. /// - Returns: An image with round corner of `self`.
  102. ///
  103. /// - Note: This method only works for CG-based image. The current image scale is kept.
  104. /// For any non-CG-based image, `base` itself is returned.
  105. public func image(withRoundRadius radius: CGFloat,
  106. fit size: CGSize,
  107. roundingCorners corners: RectCorner = .all,
  108. backgroundColor: Color? = nil) -> Image
  109. {
  110. guard let _ = cgImage else {
  111. assertionFailure("[Kingfisher] Round corner image only works for CG-based image.")
  112. return base
  113. }
  114. let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: size)
  115. return draw(to: size) { _ in
  116. #if os(macOS)
  117. if let backgroundColor = backgroundColor {
  118. let rectPath = NSBezierPath(rect: rect)
  119. backgroundColor.setFill()
  120. rectPath.fill()
  121. }
  122. let path = NSBezierPath(roundedRect: rect, byRoundingCorners: corners, radius: radius)
  123. #if swift(>=4.2)
  124. path.windingRule = .evenOdd
  125. #else
  126. path.windingRule = .evenOddWindingRule
  127. #endif
  128. path.addClip()
  129. base.draw(in: rect)
  130. #else
  131. guard let context = UIGraphicsGetCurrentContext() else {
  132. assertionFailure("[Kingfisher] Failed to create CG context for image.")
  133. return
  134. }
  135. if let backgroundColor = backgroundColor {
  136. let rectPath = UIBezierPath(rect: rect)
  137. backgroundColor.setFill()
  138. rectPath.fill()
  139. }
  140. let path = UIBezierPath(roundedRect: rect,
  141. byRoundingCorners: corners.uiRectCorner,
  142. cornerRadii: CGSize(width: radius, height: radius)).cgPath
  143. context.addPath(path)
  144. context.clip()
  145. base.draw(in: rect)
  146. #endif
  147. }
  148. }
  149. #if os(iOS) || os(tvOS)
  150. func resize(to size: CGSize, for contentMode: UIView.ContentMode) -> Image {
  151. switch contentMode {
  152. case .scaleAspectFit:
  153. return resize(to: size, for: .aspectFit)
  154. case .scaleAspectFill:
  155. return resize(to: size, for: .aspectFill)
  156. default:
  157. return resize(to: size)
  158. }
  159. }
  160. #endif
  161. // MARK: Resizing
  162. /// Resizes `base` image to an image with new size.
  163. ///
  164. /// - Parameter size: The target size in point.
  165. /// - Returns: An image with new size.
  166. /// - Note: This method only works for CG-based image. The current image scale is kept.
  167. /// For any non-CG-based image, `base` itself is returned.
  168. public func resize(to size: CGSize) -> Image {
  169. guard let _ = cgImage else {
  170. assertionFailure("[Kingfisher] Resize only works for CG-based image.")
  171. return base
  172. }
  173. let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: size)
  174. return draw(to: size) { _ in
  175. #if os(macOS)
  176. base.draw(in: rect, from: .zero, operation: .copy, fraction: 1.0)
  177. #else
  178. base.draw(in: rect)
  179. #endif
  180. }
  181. }
  182. /// Resizes `base` image to an image of new size, respecting the given content mode.
  183. ///
  184. /// - Parameters:
  185. /// - targetSize: The target size in point.
  186. /// - contentMode: Content mode of output image should be.
  187. /// - Returns: An image with new size.
  188. ///
  189. /// - Note: This method only works for CG-based image. The current image scale is kept.
  190. /// For any non-CG-based image, `base` itself is returned.
  191. public func resize(to targetSize: CGSize, for contentMode: ContentMode) -> Image {
  192. let newSize = size.kf.resize(to: targetSize, for: contentMode)
  193. return resize(to: newSize)
  194. }
  195. // MARK: Cropping
  196. /// Crops `base` image to a new size with a given anchor.
  197. ///
  198. /// - Parameters:
  199. /// - size: The target size.
  200. /// - anchor: The anchor point from which the size should be calculated.
  201. /// - Returns: An image with new size.
  202. ///
  203. /// - Note: This method only works for CG-based image. The current image scale is kept.
  204. /// For any non-CG-based image, `base` itself is returned.
  205. public func crop(to size: CGSize, anchorOn anchor: CGPoint) -> Image {
  206. guard let cgImage = cgImage else {
  207. assertionFailure("[Kingfisher] Crop only works for CG-based image.")
  208. return base
  209. }
  210. let rect = self.size.kf.constrainedRect(for: size, anchor: anchor)
  211. guard let image = cgImage.cropping(to: rect.scaled(scale)) else {
  212. assertionFailure("[Kingfisher] Cropping image failed.")
  213. return base
  214. }
  215. return KingfisherWrapper.image(cgImage: image, scale: scale, refImage: base)
  216. }
  217. // MARK: Blur
  218. /// Creates an image with blur effect based on `base` image.
  219. ///
  220. /// - Parameter radius: The blur radius should be used when creating blur effect.
  221. /// - Returns: An image with blur effect applied.
  222. ///
  223. /// - Note: This method only works for CG-based image. The current image scale is kept.
  224. /// For any non-CG-based image, `base` itself is returned.
  225. public func blurred(withRadius radius: CGFloat) -> Image {
  226. guard let cgImage = cgImage else {
  227. assertionFailure("[Kingfisher] Blur only works for CG-based image.")
  228. return base
  229. }
  230. // http://www.w3.org/TR/SVG/filters.html#feGaussianBlurElement
  231. // let d = floor(s * 3*sqrt(2*pi)/4 + 0.5)
  232. // if d is odd, use three box-blurs of size 'd', centered on the output pixel.
  233. let s = Float(max(radius, 2.0))
  234. // We will do blur on a resized image (*0.5), so the blur radius could be half as well.
  235. // Fix the slow compiling time for Swift 3.
  236. // See https://github.com/onevcat/Kingfisher/issues/611
  237. let pi2 = 2 * Float.pi
  238. let sqrtPi2 = sqrt(pi2)
  239. var targetRadius = floor(s * 3.0 * sqrtPi2 / 4.0 + 0.5)
  240. if targetRadius.isEven { targetRadius += 1 }
  241. // Determine necessary iteration count by blur radius.
  242. let iterations: Int
  243. if radius < 0.5 {
  244. iterations = 1
  245. } else if radius < 1.5 {
  246. iterations = 2
  247. } else {
  248. iterations = 3
  249. }
  250. let w = Int(size.width)
  251. let h = Int(size.height)
  252. let rowBytes = Int(CGFloat(cgImage.bytesPerRow))
  253. func createEffectBuffer(_ context: CGContext) -> vImage_Buffer {
  254. let data = context.data
  255. let width = vImagePixelCount(context.width)
  256. let height = vImagePixelCount(context.height)
  257. let rowBytes = context.bytesPerRow
  258. return vImage_Buffer(data: data, height: height, width: width, rowBytes: rowBytes)
  259. }
  260. guard let context = beginContext(size: size, scale: scale, inverting: true) else {
  261. assertionFailure("[Kingfisher] Failed to create CG context for blurring image.")
  262. return base
  263. }
  264. context.draw(cgImage, in: CGRect(x: 0, y: 0, width: w, height: h))
  265. endContext()
  266. var inBuffer = createEffectBuffer(context)
  267. guard let outContext = beginContext(size: size, scale: scale, inverting: true) else {
  268. assertionFailure("[Kingfisher] Failed to create CG context for blurring image.")
  269. return base
  270. }
  271. defer { endContext() }
  272. var outBuffer = createEffectBuffer(outContext)
  273. for _ in 0 ..< iterations {
  274. let flag = vImage_Flags(kvImageEdgeExtend)
  275. vImageBoxConvolve_ARGB8888(
  276. &inBuffer, &outBuffer, nil, 0, 0, UInt32(targetRadius), UInt32(targetRadius), nil, flag)
  277. // Next inBuffer should be the outButter of current iteration
  278. (inBuffer, outBuffer) = (outBuffer, inBuffer)
  279. }
  280. #if os(macOS)
  281. let result = outContext.makeImage().flatMap {
  282. fixedForRetinaPixel(cgImage: $0, to: size)
  283. }
  284. #else
  285. let result = outContext.makeImage().flatMap {
  286. Image(cgImage: $0, scale: base.scale, orientation: base.imageOrientation)
  287. }
  288. #endif
  289. guard let blurredImage = result else {
  290. assertionFailure("[Kingfisher] Can not make an blurred image within this context.")
  291. return base
  292. }
  293. return blurredImage
  294. }
  295. // MARK: Overlay
  296. /// Creates an image from `base` image with a color overlay layer.
  297. ///
  298. /// - Parameters:
  299. /// - color: The color should be use to overlay.
  300. /// - fraction: Fraction of input color. From 0.0 to 1.0. 0.0 means solid color,
  301. /// 1.0 means transparent overlay.
  302. /// - Returns: An image with a color overlay applied.
  303. ///
  304. /// - Note: This method only works for CG-based image. The current image scale is kept.
  305. /// For any non-CG-based image, `base` itself is returned.
  306. public func overlaying(with color: Color, fraction: CGFloat) -> Image {
  307. guard let _ = cgImage else {
  308. assertionFailure("[Kingfisher] Overlaying only works for CG-based image.")
  309. return base
  310. }
  311. let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  312. return draw(to: rect.size) { context in
  313. #if os(macOS)
  314. base.draw(in: rect)
  315. if fraction > 0 {
  316. color.withAlphaComponent(1 - fraction).set()
  317. rect.fill(using: .sourceAtop)
  318. }
  319. #else
  320. color.set()
  321. UIRectFill(rect)
  322. base.draw(in: rect, blendMode: .destinationIn, alpha: 1.0)
  323. if fraction > 0 {
  324. base.draw(in: rect, blendMode: .sourceAtop, alpha: fraction)
  325. }
  326. #endif
  327. }
  328. }
  329. // MARK: Tint
  330. /// Creates an image from `base` image with a color tint.
  331. ///
  332. /// - Parameter color: The color should be used to tint `base`
  333. /// - Returns: An image with a color tint applied.
  334. public func tinted(with color: Color) -> Image {
  335. #if os(watchOS)
  336. return base
  337. #else
  338. return apply(.tint(color))
  339. #endif
  340. }
  341. // MARK: Color Control
  342. /// Create an image from `self` with color control.
  343. ///
  344. /// - Parameters:
  345. /// - brightness: Brightness changing to image.
  346. /// - contrast: Contrast changing to image.
  347. /// - saturation: Saturation changing to image.
  348. /// - inputEV: InputEV changing to image.
  349. /// - Returns: An image with color control applied.
  350. public func adjusted(brightness: CGFloat, contrast: CGFloat, saturation: CGFloat, inputEV: CGFloat) -> Image {
  351. #if os(watchOS)
  352. return base
  353. #else
  354. return apply(.colorControl((brightness, contrast, saturation, inputEV)))
  355. #endif
  356. }
  357. /// Return an image with given scale.
  358. ///
  359. /// - Parameter scale: Target scale factor the new image should have.
  360. /// - Returns: The image with target scale. If the base image is already in the scale, `base` will be returned.
  361. public func scaled(to scale: CGFloat) -> Image {
  362. guard scale != self.scale else {
  363. return base
  364. }
  365. guard let cgImage = cgImage else {
  366. assertionFailure("[Kingfisher] Scaling only works for CG-based image.")
  367. return base
  368. }
  369. return KingfisherWrapper.image(cgImage: cgImage, scale: scale, refImage: base)
  370. }
  371. }
  372. // MARK: - Decoding Image
  373. extension KingfisherWrapper where Base: Image {
  374. /// Returns the decoded image of the `base` image. It will draw the image in a plain context and return the data
  375. /// from it. This could improve the drawing performance when an image is just created from data but not yet
  376. /// displayed for the first time.
  377. ///
  378. /// - Note: This method only works for CG-based image. The current image scale is kept.
  379. /// For any non-CG-based image or animated image, `base` itself is returned.
  380. public var decoded: Image { return decoded(scale: scale) }
  381. /// Returns decoded image of the `base` image at a given scale. It will draw the image in a plain context and
  382. /// return the data from it. This could improve the drawing performance when an image is just created from
  383. /// data but not yet displayed for the first time.
  384. ///
  385. /// - Parameter scale: The given scale of target image should be.
  386. /// - Returns: The decoded image ready to be displayed.
  387. ///
  388. /// - Note: This method only works for CG-based image. The current image scale is kept.
  389. /// For any non-CG-based image or animated image, `base` itself is returned.
  390. public func decoded(scale: CGFloat) -> Image {
  391. // Prevent animated image (GIF) losing it's images
  392. #if os(iOS)
  393. if imageSource != nil { return base }
  394. #else
  395. if images != nil { return base }
  396. #endif
  397. guard let imageRef = cgImage else {
  398. assertionFailure("[Kingfisher] Decoding only works for CG-based image.")
  399. return base
  400. }
  401. let size = CGSize(width: CGFloat(imageRef.width) / scale, height: CGFloat(imageRef.height) / scale)
  402. return draw(to: size, inverting: true, scale: scale) { context in
  403. context.draw(imageRef, in: CGRect(origin: .zero, size: size))
  404. }
  405. }
  406. }
  407. extension KingfisherWrapper where Base: Image {
  408. func beginContext(size: CGSize, scale: CGFloat, inverting: Bool = false) -> CGContext? {
  409. #if os(macOS)
  410. guard let rep = NSBitmapImageRep(
  411. bitmapDataPlanes: nil,
  412. pixelsWide: Int(size.width),
  413. pixelsHigh: Int(size.height),
  414. bitsPerSample: cgImage?.bitsPerComponent ?? 8,
  415. samplesPerPixel: 4,
  416. hasAlpha: true,
  417. isPlanar: false,
  418. colorSpaceName: .calibratedRGB,
  419. bytesPerRow: 0,
  420. bitsPerPixel: 0) else
  421. {
  422. assertionFailure("[Kingfisher] Image representation cannot be created.")
  423. return nil
  424. }
  425. rep.size = size
  426. NSGraphicsContext.saveGraphicsState()
  427. guard let context = NSGraphicsContext(bitmapImageRep: rep) else {
  428. assertionFailure("[Kingfisher] Image context cannot be created.")
  429. return nil
  430. }
  431. NSGraphicsContext.current = context
  432. return context.cgContext
  433. #else
  434. UIGraphicsBeginImageContextWithOptions(size, false, scale)
  435. guard let context = UIGraphicsGetCurrentContext() else { return nil }
  436. if inverting { // If drawing a CGImage, we need to make context flipped.
  437. context.scaleBy(x: 1.0, y: -1.0)
  438. context.translateBy(x: 0, y: -size.height)
  439. }
  440. return context
  441. #endif
  442. }
  443. func endContext() {
  444. #if os(macOS)
  445. NSGraphicsContext.restoreGraphicsState()
  446. #else
  447. UIGraphicsEndImageContext()
  448. #endif
  449. }
  450. func draw(to size: CGSize, inverting: Bool = false, scale: CGFloat? = nil, draw: (CGContext) -> Void) -> Image {
  451. let targetScale = scale ?? self.scale
  452. guard let context = beginContext(size: size, scale: targetScale, inverting: inverting) else {
  453. assertionFailure("[Kingfisher] Failed to create CG context for blurring image.")
  454. return base
  455. }
  456. defer { endContext() }
  457. draw(context)
  458. guard let cgImage = context.makeImage() else {
  459. return base
  460. }
  461. return KingfisherWrapper.image(cgImage: cgImage, scale: targetScale, refImage: base)
  462. }
  463. #if os(macOS)
  464. func fixedForRetinaPixel(cgImage: CGImage, to size: CGSize) -> Image {
  465. let image = Image(cgImage: cgImage, size: base.size)
  466. let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: size)
  467. return draw(to: self.size) { context in
  468. image.draw(in: rect, from: .zero, operation: .copy, fraction: 1.0)
  469. }
  470. }
  471. #endif
  472. }
  473. extension CGImage: KingfisherCompatible {}
  474. /// High Performance Image Resizing
  475. /// @see https://nshipster.com/image-resizing/
  476. extension KingfisherWrapper where Base: CGImage {
  477. var size: CGSize {
  478. return CGSize(width: CGFloat(base.width), height: CGFloat(base.height))
  479. }
  480. /// Resizes `base` CGImage to a CGImage of new size, respecting the given content mode.
  481. ///
  482. /// - Parameters:
  483. /// - targetSize: The target size in point.
  484. /// - contentMode: Content mode of output image should be.
  485. /// - Returns: A CGImage with new size.
  486. #if os(iOS) || os(tvOS)
  487. public func resize(to size: CGSize, for contentMode: UIView.ContentMode) -> CGImage {
  488. switch contentMode {
  489. case .scaleAspectFit:
  490. return resize(to: size, for: .aspectFit)
  491. case .scaleAspectFill:
  492. return resize(to: size, for: .aspectFill)
  493. default:
  494. return resize(to: size)
  495. }
  496. }
  497. #endif
  498. // MARK: - Resize
  499. /// Resizes `base` CGImage to a CGImage with new size.
  500. ///
  501. /// - Parameter size: The target size in point.
  502. /// - Returns: A CGImage with new size.
  503. public func resize(to size: CGSize) -> CGImage {
  504. let alphaInfo = base.alphaInfo.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
  505. var hasAlpha = false
  506. if alphaInfo == CGImageAlphaInfo.premultipliedLast.rawValue
  507. || alphaInfo == CGImageAlphaInfo.premultipliedFirst.rawValue
  508. || alphaInfo == CGImageAlphaInfo.first.rawValue
  509. || alphaInfo == CGImageAlphaInfo.last.rawValue {
  510. hasAlpha = true
  511. }
  512. var bitmapInfo = CGImageByteOrderInfo.order32Little.rawValue
  513. bitmapInfo |= hasAlpha ? CGImageAlphaInfo.premultipliedFirst.rawValue : CGImageAlphaInfo.noneSkipFirst.rawValue
  514. guard let context = CGContext(data: nil,
  515. width: Int(size.width),
  516. height: Int(size.height),
  517. bitsPerComponent: base.bitsPerComponent,
  518. bytesPerRow: base.bytesPerRow,
  519. space: base.colorSpace ?? CGColorSpaceCreateDeviceRGB(),
  520. bitmapInfo: bitmapInfo) else
  521. {
  522. return base
  523. }
  524. let rect = CGRect(origin: .zero, size: size)
  525. context.interpolationQuality = .high
  526. context.draw(base, in: rect)
  527. return context.makeImage() ?? base
  528. }
  529. /// Resizes `base` CGImage to a CGImage of new size, respecting the given content mode.
  530. ///
  531. /// - Parameters:
  532. /// - targetSize: The target size in point.
  533. /// - contentMode: Content mode of output image should be.
  534. /// - Returns: A CGImage with new size.
  535. public func resize(to targetSize: CGSize, for contentMode: ContentMode) -> CGImage {
  536. let newSize = size.kf.resize(to: targetSize, for: contentMode)
  537. return resize(to: newSize)
  538. }
  539. }