1

In SwiftUI I’m using Kingfisher dependency for image download / cashing & have a following issue, when image resized it becomes low quality and blurry.

Here’s my implementation:

struct IconView: View {
    
    let frame: CGFloat
    let url: String
    let iconPath: String

    var body: some View {
        if !iconPath.isEmpty {
            KFImage(URL(string: "\(url)\(iconPath)"))
                .placeholder {
                    Circle().fill(Color.primaryBackground)
                        .frame(width: frame, height: frame)
                }
                .setProcessor(ResizingImageProcessor(referenceSize: CGSize(width: frame , height: frame), mode: .aspectFit))
        }
    }
}

can anyone suggest right implementation? How to not destroy image quality.

Hattori Hanzō
  • 2,349
  • 4
  • 19
  • 36

1 Answers1

0

I think I solved my problem, issue here is that Kingfisher calculates image size in pixels rather than points in ResizingImageProcessor, so you have to multiply image size to device scale factor.

Useful discussions: IOS Swift Kingfisher Resize processor result blurry image & ResizingImageProcessor return a low quality image

struct IconView: View {
    
    let frame: CGFloat
    let url: String
    let iconPath: String

    var body: some View {
        if !iconPath.isEmpty {
            KFImage(URL(string: "\(url)\(iconPath)"))
                .placeholder {
                    Circle().fill(Color.primaryBackground)
                        .frame(width: frame, height: frame)
                }
                .resizable()
                .setProcessor(ResizingImageProcessor(referenceSize: CGSize(width: frame * scale, height: frame * scale), mode: .aspectFit))
                .frame(width: frame, height: frame)
        }
    }
    
    private var scale: CGFloat {
        UIScreen.main.scale
    }
}
Hattori Hanzō
  • 2,349
  • 4
  • 19
  • 36