iOS Question Who can help me to create b4i library?

Brandsum

Well-Known Member
Licensed User
As far as I can remember I have used something from github to convert webp to png. I have to search that. I will move those codes to a class and will post tonight or tomorrow.
 
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
You cannot use "jar" libraries in iOS.

I try to wrap an object c method named pngwithwebp,but when i use it in b4i, Warning show:Types do not match,and debug error:
B4X:
Application_Start
Application_Background
Application_Foreground
Application_Active
Error occurred on line: 164 (Main)
-[UIImage imageWithWebp::]: unrecognized selector sent to instance 0x282618700
Stack Trace: (
  CoreFoundation       <redacted> + 252
  libobjc.A.dylib      objc_exception_throw + 56
  CoreFoundation       <redacted> + 0
  CoreFoundation       <redacted> + 1408
  CoreFoundation       _CF_forwarding_prep_0 + 92
  B4i Example          -[b4i_main _button2_click] + 788
  CoreFoundation       <redacted> + 144
  CoreFoundation       <redacted> + 292
  B4i Example          +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1624
  B4i Example          -[B4IShell runMethod:] + 448
 B4i Example          -[B4IShell raiseEventImpl:method:args::] + 1648
 B4i Example          -[B4IShellBI raiseEvent:event:params:] + 1580
 B4i Example          __33-[B4I raiseUIEvent:event:params:]_block_invoke + 60
 libdispatch.dylib    <redacted> + 24
 libdispatch.dylib    <redacted> + 16
 libdispatch.dylib    <redacted> + 1068
 CoreFoundation       <redacted> + 12
 CoreFoundation       <redacted> + 1924
 CoreFoundation       CFRunLoopRunSpecific + 436
 GraphicsServices     GSEventRunModal + 104
 UIKitCore            UIApplicationMain + 212
 B4i Example          main + 124
 libdyld.dylib        <redacted> + 4
)

My b4i code:
B4X:
Dim w2p As webp2png
Dim ss As Bitmap  ss=w2p.imageWithWebp(File.Combine(File.DirTemp,"1.webp"))
IV.Bitmap=ss
'IV is ImageView

Hi Erel,can you give me some advices? thank you.
 

Attachments

  • pngwithwebp.zip
    51.4 KB · Views: 353
Last edited:
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
Something is wrong with your library. The method name includes two colons and there is only one parameter.

It will probably be simpler to use NativeObject to access the native library.
Thank you,it depend on 3rd part framework, can't use NativeObject. I upload the project file, I don't know where the problem is.
you can download the 3rd part framework from https://storage.googleapis.com/down...eases/webp/libwebp-1.0.0-ios-framework.tar.gz,it is WebpFramework-1.0.0.
 
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
That's wrong. You can add a reference to the framework with #AdditionalLib.

Thank you,Erel, Using NativeObject is a bit difficult for me,for the AdditionalLib is only the part of the object-c library(Written by others),I have no ability to modify too much.
The good news is that my first wrapping is successful via modifying only a few object-c library code.Webp image is successfully decoded and displayed in IOS imageview.
My goal is to decode the webp image and display in the wkwebview.My idea is to save the image on imageview to File.DirDocuments or other directory first,then show on wkwebview via LoadUrl method. I find these code from b4a,but not working and no error in b4i:
B4X:
Dim bmpImage As Bitmap
Dim Canvas1 As B4XCanvas
Dim out As OutputStream
Dim IV as ImageView
Dim WKWeb as wkwebview
If bmpImage.IsInitialized Then
    Canvas1.Initialize(bmpImage)
    IV.Bitmap=bmpImage
    out = File.OpenOutput(File.DirDocuments, "1.jpg", False)
    bmpImage.WriteToStream(out,100,"JPEG")
    out.Close
end if
WKWeb.LoadUrl($"${xui.FileUri(File.DirDocuments,"1.jpg")}"$)
I use File.Exists method to see if 1.jpg exists and the result does not exist and wkweb don't load 1.jpg. I don't know where the problem is.
 
Last edited:
Upvote 0

Brandsum

Well-Known Member
Licensed User
Copy WebP.framework to your local build server's Libs folder
Add this to your project #AdditionalLib: WebP.framework
Paste this objc code at the bottom
B4X:
#if objc
#import <WebP/decode.h>

-(UIImage *)convertWebPToUIimage: (NSString *) filePath{
    NSData *myData = [NSData dataWithContentsOfFile:filePath];
    int width = 0;
    int height = 0;
    WebPGetInfo([myData bytes], [myData length], &width, &height);
    uint8_t *data = WebPDecodeRGBA([myData bytes], [myData length], &width, &height);
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, width*height*4, free_image_data);
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4*width, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    return [UIImage imageWithCGImage:imageRef];
}

static void free_image_data(void *info, const void *data, size_t size)
{
    if(info != NULL)
    {WebPFreeDecBuffer(&(((WebPDecoderConfig *)info)->output));}
    else
    {free((void *)data);}
}

#End If

Then use it like below
B4X:
Dim no As NativeObject = Me
Dim bmp As Bitmap = no.RunMethod("convertWebPToUIimage:",Array(File.Combine(File.DirAssets,"1.webp")))

Dim Out As OutputStream
Out = File.OpenOutput(File.DirDocuments, "image.png", False)
bmp.WriteToStream(Out, 100, "PNG")
Out.Close
 
Last edited:
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
Hi Brandsum ,many thanks, You gave me the method that Erel said.I test it and works. Now we have two ways to use webp image in IOS.Of course, using
RunMethod is simpler.
 
Upvote 0
Top