|
@@ -343,13 +343,49 @@ public struct CompositingImageProcessor: ImageProcessor {
|
|
|
/// case.
|
|
/// case.
|
|
|
///
|
|
///
|
|
|
public struct RoundCornerImageProcessor: ImageProcessor {
|
|
public struct RoundCornerImageProcessor: ImageProcessor {
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /// Represents a radius specified in a `RoundCornerImageProcessor`.
|
|
|
|
|
+ public enum Radius {
|
|
|
|
|
+ /// The radius should be calculated as a fraction of the image width. Typically the associated value should be
|
|
|
|
|
+ /// between 0 and 0.5, where 0 represents no radius and 0.5 represents using half of the image width.
|
|
|
|
|
+ case widthFraction(CGFloat)
|
|
|
|
|
+ /// The radius should be calculated as a fraction of the image height. Typically the associated value should be
|
|
|
|
|
+ /// between 0 and 0.5, where 0 represents no radius and 0.5 represents using half of the image height.
|
|
|
|
|
+ case heightFraction(CGFloat)
|
|
|
|
|
+ /// Use a fixed point value as the round corner radius.
|
|
|
|
|
+ case point(CGFloat)
|
|
|
|
|
+
|
|
|
|
|
+ var radiusIdentifier: String {
|
|
|
|
|
+ switch self {
|
|
|
|
|
+ case .widthFraction(let f):
|
|
|
|
|
+ return "w_frac_\(f)"
|
|
|
|
|
+ case .heightFraction(let f):
|
|
|
|
|
+ return "h_frac_\(f)"
|
|
|
|
|
+ case .point(let p):
|
|
|
|
|
+ return p.description
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/// Identifier of the processor.
|
|
/// Identifier of the processor.
|
|
|
/// - Note: See documentation of `ImageProcessor` protocol for more.
|
|
/// - Note: See documentation of `ImageProcessor` protocol for more.
|
|
|
public let identifier: String
|
|
public let identifier: String
|
|
|
|
|
|
|
|
- /// Corner radius will be applied in processing.
|
|
|
|
|
- public let cornerRadius: CGFloat
|
|
|
|
|
|
|
+ /// Corner radius will be applied in processing. To provide backward compatibility, this property returns `0` unless
|
|
|
|
|
+ /// `Radius.point` is specified.
|
|
|
|
|
+ @available(*, deprecated, message: "Use `radius` property instead.")
|
|
|
|
|
+ public var cornerRadius: CGFloat {
|
|
|
|
|
+ switch radius {
|
|
|
|
|
+ case .widthFraction, .heightFraction:
|
|
|
|
|
+ return 0.0
|
|
|
|
|
+ case .point(let value):
|
|
|
|
|
+ return value
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// The radius will be applied in processing. Specify a certain point value with `.point`, or a fraction of the
|
|
|
|
|
+ /// target image with `.fraction`. `.fraction(0.5)` means use half of the
|
|
|
|
|
+ public let radius: Radius
|
|
|
|
|
|
|
|
/// The target corners which will be applied rounding.
|
|
/// The target corners which will be applied rounding.
|
|
|
public let roundingCorners: RectCorner
|
|
public let roundingCorners: RectCorner
|
|
@@ -363,19 +399,47 @@ public struct RoundCornerImageProcessor: ImageProcessor {
|
|
|
/// Creates a `RoundCornerImageProcessor`.
|
|
/// Creates a `RoundCornerImageProcessor`.
|
|
|
///
|
|
///
|
|
|
/// - Parameters:
|
|
/// - Parameters:
|
|
|
- /// - cornerRadius: Corner radius will be applied in processing.
|
|
|
|
|
|
|
+ /// - cornerRadius: Corner radius in point will be applied in processing.
|
|
|
/// - targetSize: Target size of output image should be. If `nil`,
|
|
/// - targetSize: Target size of output image should be. If `nil`,
|
|
|
/// the image will keep its original size after processing.
|
|
/// the image will keep its original size after processing.
|
|
|
/// Default is `nil`.
|
|
/// Default is `nil`.
|
|
|
/// - corners: The target corners which will be applied rounding. Default is `.all`.
|
|
/// - corners: The target corners which will be applied rounding. Default is `.all`.
|
|
|
/// - backgroundColor: Background color to apply for the output image. Default is `nil`.
|
|
/// - backgroundColor: Background color to apply for the output image. Default is `nil`.
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// - Note:
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// This initializer accepts a concrete point value for `cornerRadius`. If you do not know the image size, but still
|
|
|
|
|
+ /// want to apply a full round-corner (making the final image a round one), or specify the corner radius as a
|
|
|
|
|
+ /// fraction of one dimension of the target image, use the `Radius` version instead.
|
|
|
|
|
+ ///
|
|
|
public init(
|
|
public init(
|
|
|
cornerRadius: CGFloat,
|
|
cornerRadius: CGFloat,
|
|
|
targetSize: CGSize? = nil,
|
|
targetSize: CGSize? = nil,
|
|
|
roundingCorners corners: RectCorner = .all,
|
|
roundingCorners corners: RectCorner = .all,
|
|
|
- backgroundColor: KFCrossPlatformColor? = nil)
|
|
|
|
|
|
|
+ backgroundColor: KFCrossPlatformColor? = nil
|
|
|
|
|
+ )
|
|
|
|
|
+ {
|
|
|
|
|
+ let radius = Radius.point(cornerRadius)
|
|
|
|
|
+ self.init(radius: radius, targetSize: targetSize, roundingCorners: corners, backgroundColor: backgroundColor)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// Creates a `RoundCornerImageProcessor`.
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// - Parameters:
|
|
|
|
|
+ /// - radius: The radius will be applied in processing.
|
|
|
|
|
+ /// - targetSize: Target size of output image should be. If `nil`,
|
|
|
|
|
+ /// the image will keep its original size after processing.
|
|
|
|
|
+ /// Default is `nil`.
|
|
|
|
|
+ /// - corners: The target corners which will be applied rounding. Default is `.all`.
|
|
|
|
|
+ /// - backgroundColor: Background color to apply for the output image. Default is `nil`.
|
|
|
|
|
+ public init(
|
|
|
|
|
+ radius: Radius,
|
|
|
|
|
+ targetSize: CGSize? = nil,
|
|
|
|
|
+ roundingCorners corners: RectCorner = .all,
|
|
|
|
|
+ backgroundColor: KFCrossPlatformColor? = nil
|
|
|
|
|
+ )
|
|
|
{
|
|
{
|
|
|
- self.cornerRadius = cornerRadius
|
|
|
|
|
|
|
+ self.radius = radius
|
|
|
self.targetSize = targetSize
|
|
self.targetSize = targetSize
|
|
|
self.roundingCorners = corners
|
|
self.roundingCorners = corners
|
|
|
self.backgroundColor = backgroundColor
|
|
self.backgroundColor = backgroundColor
|
|
@@ -385,10 +449,10 @@ public struct RoundCornerImageProcessor: ImageProcessor {
|
|
|
|
|
|
|
|
if let size = targetSize {
|
|
if let size = targetSize {
|
|
|
identifier = "com.onevcat.Kingfisher.RoundCornerImageProcessor" +
|
|
identifier = "com.onevcat.Kingfisher.RoundCornerImageProcessor" +
|
|
|
- "(\(cornerRadius)_\(size)\(corners.cornerIdentifier))"
|
|
|
|
|
|
|
+ "(\(radius.radiusIdentifier)_\(size)\(corners.cornerIdentifier))"
|
|
|
} else {
|
|
} else {
|
|
|
identifier = "com.onevcat.Kingfisher.RoundCornerImageProcessor" +
|
|
identifier = "com.onevcat.Kingfisher.RoundCornerImageProcessor" +
|
|
|
- "(\(cornerRadius)\(corners.cornerIdentifier))"
|
|
|
|
|
|
|
+ "(\(radius.radiusIdentifier)\(corners.cornerIdentifier))"
|
|
|
}
|
|
}
|
|
|
if let backgroundColor = backgroundColor {
|
|
if let backgroundColor = backgroundColor {
|
|
|
identifier += "_\(backgroundColor)"
|
|
identifier += "_\(backgroundColor)"
|
|
@@ -410,6 +474,17 @@ public struct RoundCornerImageProcessor: ImageProcessor {
|
|
|
switch item {
|
|
switch item {
|
|
|
case .image(let image):
|
|
case .image(let image):
|
|
|
let size = targetSize ?? image.kf.size
|
|
let size = targetSize ?? image.kf.size
|
|
|
|
|
+
|
|
|
|
|
+ let cornerRadius: CGFloat
|
|
|
|
|
+ switch radius {
|
|
|
|
|
+ case .point(let point):
|
|
|
|
|
+ cornerRadius = point
|
|
|
|
|
+ case .widthFraction(let widthFraction):
|
|
|
|
|
+ cornerRadius = size.width * widthFraction
|
|
|
|
|
+ case .heightFraction(let heightFraction):
|
|
|
|
|
+ cornerRadius = size.height * heightFraction
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return image.kf.scaled(to: options.scaleFactor)
|
|
return image.kf.scaled(to: options.scaleFactor)
|
|
|
.kf.image(
|
|
.kf.image(
|
|
|
withRoundRadius: cornerRadius,
|
|
withRoundRadius: cornerRadius,
|