3

I am currently doing a sample iOS mobile app(Xcode 7, Swift 2.2) which does both encoding and decoding of WebP images. I have downloaded a precompiled version of WebP framework and included in my project using a bridging header file. It works fine while decoding a WebP image using WebPDecodeRGBA. But when I use the WebPEncodeRGB to convert a jpg image into WebP format it shows compile time error as follows:

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)

I have added the code for encoding as a function of viewcontroller class as follows:

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)
  }

Also, is there any other way to convert a jpg image to WebP format using Swift?

Hasya
  • 9,792
  • 4
  • 31
  • 46

0 Answers0