Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract data from processor .data and pass to .image #2191

Closed
missphyrgames opened this issue Jan 6, 2024 · 2 comments
Closed

Extract data from processor .data and pass to .image #2191

missphyrgames opened this issue Jan 6, 2024 · 2 comments

Comments

@missphyrgames
Copy link

Hello, I'm trying to extract some data from a processor in the .data case and pass it along to the .image case. For example, I have following code:

struct  CustomProcessor: ImageProcessor {
   
    let identifier: String

    func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
        switch item {
        case .image(let image):
            // want to use metadata here
            // do something
            return image
        case .data:
            // I want to extract some metadata from the image here
           let src = CGImageSourceCreateWithData(data as CFData, nil)

           // how can I use this in the .image case when we pass back the data to the DefaultImageProcessor?
           let metadata = CGImageSourceCopyMetadataAtIndex(src!, 0, nil)

            return (DefaultImageProcessor.default |> self).process(item: item, options: options)
        }
    }
}

Any help would be appreciated!

@onevcat
Copy link
Owner

onevcat commented Jan 8, 2024

I am not sure if it is an abusing of ImageProcessor, but how about these code?

struct MetaDataProcessor: ImageProcessor {
    let metaData: MetaData // can be an int, or anything else you can customize.
    var identifier: String {  /* It is better to return a value can be identified by the metaData.  */  }

    func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
        switch item {
        case .image(let image):
            // You now have the correct metaData here.
            return image
        case .data:
            // You can even leave empty, this case won't be called.
            return UIImage()
        }
    }
}

struct CustomProcessor: ImageProcessor {
   
    let identifier: String

    func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
        switch item {
        case .image(let image):
            return image
        case .data:
           let src = CGImageSourceCreateWithData(data as CFData, nil)
           let metadata = CGImageSourceCopyMetadataAtIndex(src!, 0, nil)
           let next = MetaDataProcessor(metaData: metadata)
           return (DefaultImageProcessor.default |> next).process(item: item, options: options)
        }
    }
}

And in the use case, make sure you set the CustomProcessor as the first of the .processor option.

@missphyrgames
Copy link
Author

Thank you so much @onevcat!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants