dmytro-anokhin / url-image

AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.
MIT License
1.1k stars 96 forks source link

The identifier passed in the URLImageOptions object do not pass in the initializer of URLImage #139

Closed kostastsi closed 3 years ago

kostastsi commented 3 years ago

Summary and/or background I tried to pass an identifier in the URLImageOptions object, but noticed that every time the image is getting downloaded. Debugged the URLImage and noticed that the public initializers call the private initializer of URLImage without injecting the options object. So the initializer is always getting the URLImageService.shared.defaultOptions.

Version of URLImage library v. 2.0 - The bug is reproducible in the latest version.

So I use the initializer and pass an object to options URLImageOptions(identifier: photoId)

URLImage(url: url,
           options: URLImageOptions(identifier: photoId),
           content: { image, imageInfo in
                                image
                                .resizable()
                                .scaledToFill()
                                .frame(width: avatarWidth, height: avatarHeight)
                                .clipped()
                                .clipShape(Circle())
          })

But the identifier ends up to 'nil' in the 'getImage' function of InMemoryCache class. Noticed that these two public initialziers do not pass the options object

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public extension URLImage {

    init(url: URL,
         options: URLImageOptions = URLImageService.shared.defaultOptions,
         empty: @escaping () -> Empty,
         inProgress: @escaping (_ progress: Float?) -> InProgress,
         failure: @escaping (_ error: Error, _ retry: @escaping () -> Void) -> Failure,
         content: @escaping (_ image: Image) -> Content) {

        self.init(url: url, empty: empty, inProgress: inProgress, failure: failure) { (transientImage: TransientImageType) -> Content in
            content(transientImage.image)
        }
    }

    init(url: URL,
         options: URLImageOptions = URLImageService.shared.defaultOptions,
         empty: @escaping () -> Empty,
         inProgress: @escaping (_ progress: Float?) -> InProgress,
         failure: @escaping (_ error: Error, _ retry: @escaping () -> Void) -> Failure,
         content: @escaping (_ image: Image, _ info: ImageInfo) -> Content) {

        self.init(url: url, empty: empty, inProgress: inProgress, failure: failure) { (transientImage: TransientImageType) -> Content in
            content(transientImage.image, transientImage.info)
        }
    }
}
URLImage initializer

Is that an issue, or you had something different in mind? Do I have other option of passing an identifier, cause I am not covered by using URL as identifier for caching images.

dmytro-anokhin commented 3 years ago

Hey, this is certainly not intended. Thank you for noticing and detailed report. Fixed in 2.2.2.

kostastsi commented 3 years ago

Great @dmytro-anokhin !Thank you!

kostastsi commented 3 years ago

@dmytro-anokhin Could you please update the podspec (for the newer version) too, whenever is possible? Thank you in advance!

dmytro-anokhin commented 3 years ago

sure:

--------------------------------------------------------------------------------
 πŸŽ‰  Congrats

 πŸš€  URLImage (2.2.3) successfully published
 πŸ“…  January 31st, 20:32
 🌎  https://cocoapods.org/pods/URLImage
 πŸ‘  Tell your friends!
--------------------------------------------------------------------------------