我目前正在做一个样本iOS移动应用程序(Xcode 7,Swift 2.2),它可以对WebP图像进行编码和解码。我已经下载了一个预编译版本的WebP框架,并使用桥接头文件包含在我的项目中。使用WebPDecodeRGBA解码WebP图像时,它工作正常。但是,当我使用WebPEncodeRGB将jpg图像转换为WebP格式时,它显示编译时错误如下:
Undefined symbols for architecture i386:
"_WebPEncodeRGB", referenced from:
static (extension in WebpImageTest):__ObjC.UIImage.imageToWebPData (__ObjC.NSData) -> __ObjC.UIImage? in ViewController.o
"_WebPEncodeRGB", referenced from:
WebpImageTest.ViewController.imageToWebPData (__ObjC.NSData) -> __ObjC.UIImage? in ViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我已经将编码代码添加为viewcontroller类的函数,如下所示:
func imageToWebPData(jpgImageData:NSData) -> UIImage? {
let imag = UIImage(data: jpgImageData)
let compressionLevel = 10
let inputImageWidth = imag!.size.width
let inputImageHeight = imag!.size.height
let image:CGImage = imag!.CGImage!
let provider: CGDataProviderRef? = CGImageGetDataProvider(image)
let bitmap: CFDataRef? = CGDataProviderCopyData(provider)
let rgb: UnsafePointer<UInt8> = CFDataGetBytePtr(bitmap)
let width: Int32 = Int32(inputImageWidth)
let height: Int32 = Int32(inputImageHeight)
let stride: Int32 = Int32(CGImageGetBytesPerRow(image))
let qualityFactor: Float = Float(compressionLevel)
var webp: NSData
var output: UnsafeMutablePointer<UInt8> = nil
var size: size_t = 0
size = WebPEncodeRGB(rgb,width,height,stride,qualityFactor,&output)
let afterByteLength = Int(size)
webp = NSData(bytes: output, length: afterByteLength)
free(output)
return UIImage(data: webp)
}
另外,有没有其他方法可以使用Swift将jpg图像转换为WebP格式?