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

It's possible to replace a downloaded image to other processed just after and render it with WebImage? #217

Closed vkbest closed 2 years ago

vkbest commented 2 years ago

Sorry for my poor English.

I'm using WebImage with SwiftUI on AppleTV.

The thing is, for security reasons, we are obfuscating images (I´m not sure if this word is correct), so the images on the server have different size to the real image and they are altered to make harder people download them, on web we are using Javascript and canvas to rebuild the real image from the altered image in server.

So my question, it's possible to download the image with WebImage, alter it and render with WebImage?

Thanks

dreampiggy commented 2 years ago

You can use solutions based on your use case ? https://github.com/SDWebImage/SDWebImage/wiki/Advanced-Usage

  1. Response modifier, which pass you the (URLResponse, Data) in, and you calculate and pass the (URLResponse, Data) out. So you can modifiy the original image data at here.

  2. Custom decoder, which means you can wrap you own decoder, if you use some custom encode && decode compression algorithm

  3. Custom loader, final fork. Which let you re-write the SDWebImageDownloader by your hand. Powerful but considered at the last chance.

vkbest commented 2 years ago

Hi thanks for the reply.

I tried implement a custom decoder, but is not called often, only some images trigger my decoder, so its not working to me. It's the first time I tried to implement a custom decoder, but I'm not sure this is the correct way, because I´m not really decoding/decompressing the image on any way, the images only are tiled and unordered making a similar feeling to a puzzle to avoid people downloading them but they are normal JPEG images, so its possible the custom decoder is only called on some images who requires special decoding, because the decoder is always called on the same images.

I´m not sure about the first thing you suggested, what do you mean about URLresponse? I didn't see anything about it on the documentation.

The third option looks much more work that implementing it with apple native apis.

EDIT: According the documentation I should conform the protocol SDWebImageCoder, but I can't find that protocol and I'm conforming SDImageCoder.

EDIT2: It's protocol SDImageIOCoder what is capturing the most images, some way to give priority to my custom decoder?

EDIT3: When the image is cached on disk, uses the original downloaded image.

dreampiggy commented 2 years ago

For custom decoder, there are two way to load:

  1. Register it onto SDImageCodersManager, which is the default shared instance, and manage order by each decoder one by one (ask the decoder canDecodeImageForData:, if return YES, then decode it)
  2. Load your special image using sd_setImage(with: url, placeholderImage: something, options: [], context: [.imageCoder: MyCustomJPEGDecoder.shared]), this context will only make MyCustomJPEGDecoder works for this URL.
dreampiggy commented 2 years ago

When the image is cached on disk, uses the original downloaded image.

If you want to do this, it's a little hard, but possible.

Your use case maybe:

  1. I have a encrypted image data on the network, first time when cache miss, I need to download the encrypted data, decrypt to get image, and store the decrypted data into disk
  2. When the decrypted data is already on disk, do not decrypt again (or you can use some file magic bits to check), just use normal decoder and get the image

For 1, there are something called SDWebImageCacheSerializer. Just call image.sd_imageData(as: .JPEG) and return it.

vkbest commented 2 years ago

Thanks

I implemented SDWebImageCacheSerializer and its working as should it