SDWebImage / SDWebImageSwiftUI

SwiftUI Image loading and Animation framework powered by SDWebImage
https://sdwebimage.github.io/SDWebImageSwiftUI
MIT License
2.14k stars 219 forks source link

sd_colors(with: returns nil #212

Closed arbyruns closed 2 years ago

arbyruns commented 2 years ago

I'm trying to use sd_colors(with: ) and getting back nil. Here's the code that I'm using and I've tried a few different CGRect values and I get the same thing. I know I'm getting an image back because I can see the image and I can successfully use image.sd_color(at: CGPoint(x: 50, y: 50)) and get back a color.

WebImage(url: URL(string: logURL))
    .onSuccess { image, _, _ in
        image.sd_colors(with: CGRect(x: 0, y: 50, width: 128, height: 128))
    }
Alex-Coetzee commented 2 years ago

hi, you need to force unwrap your URL, change URL(string: logURL) to URL(string: logURL)!

dreampiggy commented 2 years ago

For this issue, a demo url is better to reproduce this and fix (maybe) bugs.

The unit test in our repo for sd_color(with:) runs correctly. So this must be some rare case.

arbyruns commented 2 years ago

I forced unwrapped the URL and I'm still getting back nil. Here's my sample code with a URL.

   var body: some View {
        VStack {
            GeometryReader { geo in
                let frame = geo.frame(in: CoordinateSpace.local)
            WebImage(url: URL(string: "https://logo.clearbit.com/google.com")!)
                .onSuccess { image, _, _ in
                    print("webImage \(String(describing: image.sd_color(at: CGPoint(x: frame.origin.x + 40, y: frame.origin.y + 40))))")
                    print("webImage \(String(describing: image.sd_colors(with: CGRect(x: frame.origin.x + 40, y: frame.origin.y + 40, width: geo.size.width, height: geo.size.height))))")
                  }
            Text("Hello, World!")
                    .position(x: frame.origin.x + 40, y: frame.origin.y + 40)
            }
        }
    }

Print console:

webImage Optional(UIExtendedSRGBColorSpace 0.980392 0.992157 0.984314 1)
webImage nil
dreampiggy commented 2 years ago

I guess you misunderstand the API of sd_colors(at:) point.

The point is based on the Image itself resolution and space. So, you should use CGPoint(x: 40, y: 40), not the frame origin.

dreampiggy commented 2 years ago

sd_colors(at:) has only image information, not know about the coordinate space of the container view. (For example, 100 UIView can host single UIImage at the same time :))