free(UnsafeMutablePointer<Void>(data)) } extension UIImage { static func imageFromWebPData(data: NSData) -> UIImage? { var config = WebPDecoderConfig() if WebPInitDecoderConfig(&config) == 0 { return nil } if WebPGetFeatures(UnsafePointer<UInt8>(data.bytes), data.length, &config.input) != VP8_STATUS_OK { return nil } config.output.colorspace = config.input.has_alpha != 0 ? MODE_rgbA : MODE_RGB if WebPDecode(UnsafePointer<UInt8>(data.bytes), data.length, &config) != VP8_STATUS_OK { return nil } let width = config.input.width let height = config.input.height let provider = CGDataProviderCreateWithData(nil, config.output.u.RGBA.rgba, config.output.u.RGBA.size, freeImageData) let colorSpaceRef = CGColorSpaceCreateDeviceRGB() let components = config.input.has_alpha != 0 ? 4 : 3 let bi = config.input.has_alpha != 0 ? CGBitmapInfo(rawValue: CGBitmapInfo.ByteOrderDefault.rawValue | CGImageAlphaInfo.PremultipliedLast.rawValue) : CGBitmapInfo.ByteOrderMask guard let imageRef = CGImageCreate( Int(width), Int(height), 8, components * 8, components * Int(width), colorSpaceRef, bi, provider, nil, true, .RenderingIntentDefault) else { return nil } return UIImage(CGImage: imageRef) } }