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

Please add support for webp #377

Open
shoaibhussan15 opened this issue Jul 19, 2019 · 1 comment
Open

Please add support for webp #377

shoaibhussan15 opened this issue Jul 19, 2019 · 1 comment

Comments

@shoaibhussan15
Copy link

Please add support for webp format with SDWebImage.

@pilgrim1385
Copy link

I Use SDWebImageWebPCoder solved this problem.
Create a new class, code copy from SKPhoto. Change "open func loadUnderlyingImageAndNotify()" this function's code.

import UIKit
import SKPhotoBrowser
import SDWebImageWebPCoder

/// Copy From SKPhoto,修改了loadUnderlyingImageAndNotify()方法,以支持WebP格式的图片。
open class JFPhoto: NSObject, SKPhotoProtocol {
    open var index: Int = 0
    open var underlyingImage: UIImage!
    open var caption: String?
    open var contentMode: UIView.ContentMode = .scaleAspectFill
    open var shouldCachePhotoURLImage: Bool = false
    open var photoURL: String!

    override init() {
        super.init()
    }
    
    convenience init(image: UIImage) {
        self.init()
        underlyingImage = image
    }
    
    convenience init(url: String) {
        self.init()
        photoURL = url
    }
    
    convenience init(url: String, holder: UIImage?) {
        self.init()
        photoURL = url
        underlyingImage = holder
    }
    
    open func checkCache() {
        guard let photoURL = photoURL else {
            return
        }
        guard shouldCachePhotoURLImage else {
            return
        }
        
        if SKCache.sharedCache.imageCache is SKRequestResponseCacheable {
            let request = URLRequest(url: URL(string: photoURL)!)
            if let img = SKCache.sharedCache.imageForRequest(request) {
                underlyingImage = img
            }
        } else {
            if let img = SKCache.sharedCache.imageForKey(photoURL) {
                underlyingImage = img
            }
        }
    }
    
    open func loadUnderlyingImageAndNotify() {
        guard photoURL != nil, let URL = URL(string: photoURL) else { return }
        
        // Fetch Image
        let session = URLSession(configuration: SKPhotoBrowserOptions.sessionConfiguration)
            var task: URLSessionTask?
            task = session.dataTask(with: URL, completionHandler: { [weak self] (data, response, error) in
                guard let `self` = self else { return }
                defer { session.finishTasksAndInvalidate() }

                guard error == nil else {
                    DispatchQueue.main.async {
                        self.loadUnderlyingImageComplete()
                    }
                    return
                }

                if let data = data, let response = response {
                    // 当系统UIImage加载data为nil时,尝试使用webPCoder加载
                    var image = UIImage(data: data)
                    var tempData: Data? = data
                    if image == nil, SDImageWebPCoder.shared.canDecode(from: data) {
                        image = SDImageWebPCoder.shared.decodedImage(with: data, options: nil)
                        if image != nil {
                            tempData = image?.pngData()
                        }
                    }
                    if let finalImage = image, let finalData = tempData {
                        if self.shouldCachePhotoURLImage {
                            if SKCache.sharedCache.imageCache is SKRequestResponseCacheable {
                                SKCache.sharedCache.setImageData(finalData, response: response, request: task?.originalRequest)
                            } else {
                                SKCache.sharedCache.setImage(finalImage, forKey: self.photoURL)
                            }
                        }
                        DispatchQueue.main.async {
                            self.underlyingImage = finalImage
                            self.loadUnderlyingImageComplete()
                        }
                    }
                }
                
            })
            task?.resume()
    }

    open func loadUnderlyingImageComplete() {
        NotificationCenter.default.post(name: Notification.Name(rawValue: SKPHOTO_LOADING_DID_END_NOTIFICATION), object: self)
    }
    
}

// MARK: - Static Function

extension JFPhoto {
    public static func photoWithImage(_ image: UIImage) -> JFPhoto {
        return JFPhoto(image: image)
    }
    
    public static func photoWithImageURL(_ url: String) -> JFPhoto {
        return JFPhoto(url: url)
    }
    
    public static func photoWithImageURL(_ url: String, holder: UIImage?) -> JFPhoto {
        return JFPhoto(url: url, holder: holder)
    }
}

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