2

how can I include some pictures for the native ios [ in codename one ] and how can I use them from inside the code , I tried to create a folder Called Resources like what XCode do by default , and it didn't work code used :

[UIImage imageNamed:@"xxxx.png"];

Regards,

after trying to include the sources I looked everywhere for the images and I didn't find any [ tried to put them inside the src and inside native and inside ios folder ]

after trying to work with gui from native I have the following messages on the log : 2017-03-16 15:23:25.509 MyApplication[32748:1247180] Warning: Attempt to present on whose view is not in the window hierarchy!

AhMaD AbUIeSa
  • 805
  • 1
  • 12
  • 21
  • check this http://stackoverflow.com/questions/10639766/how-does-codename-one-work?rq=1 – Jigar Mar 14 '17 at 14:01
  • @Jigar it doesn't state anything on how to do what I want , thanks for the reference – AhMaD AbUIeSa Mar 14 '17 at 14:07
  • You can place the image file within the src directory or if you want it only for iOS within the native/iphone directory and it should be packaged into the final app and should work with imageNamed seamlessly. – Shai Almog Mar 15 '17 at 05:43
  • @ShaiAlmog so instead of using the xcode convention should I rename the folder from Resources to iphone or just put them without any sub directory , I already tried that and it didn't recognize them , should I change the code – AhMaD AbUIeSa Mar 15 '17 at 06:06
  • I think it should work without any subdirectory but I haven't tried it in a while. You can use include source and look at this within xcode. Notice you can also use our Image objects and convert them to UIImage which is what we do in some cases. E.g. if you look at the source of the GoogleMaps cn1lib you will notice that a marker is passed as an encoded image and converted to UIImage – Shai Almog Mar 15 '17 at 06:17
  • @ShaiAlmog I will check that code and try to do that – AhMaD AbUIeSa Mar 15 '17 at 06:23
  • @ShaiAlmog still no luck any ideas can't seem to make it work [ the native code alone without – AhMaD AbUIeSa Mar 16 '17 at 07:23
  • still the same problem can't find any images in the included sources from the debug build iOS – AhMaD AbUIeSa Mar 16 '17 at 11:53
  • If you placed the images in the src they should be within the res folder there. Here is the relevant code in the native section of Google Maps https://github.com/codenameone/codenameone-google-maps/blob/master/GoogleMaps/native/ios/com_codename1_googlemaps_InternalNativeMapsImpl.m#L34-L45 param is a `byte[]` value. It's invoked like this from user code: https://github.com/codenameone/codenameone-google-maps/blob/master/GoogleMaps/src/com/codename1/googlemaps/MapContainer.java#L407-L411 – Shai Almog Mar 17 '17 at 05:45
  • I get that , but in doing so I need to add lots of parameters in order to make the design work in the native code , which also will be reflected to android as well , [which is working fine without any of these, and no need to add any of the images to that part of the app ,] if I change the native interface , it will be for all platforms , what I mean is , there aren't any easier way to make the native code do that automatically and recognize the files that were placed in that directory – AhMaD AbUIeSa Mar 17 '17 at 07:31

2 Answers2

2

If you want to bundle images with your Codename One App, there are at least 2 ways of doing that:

1:

Add the images to the src folder in your project and get them as follows:

public static Image getImageFromSrcFolder(String imageName) {
    try {
        Image image = Image.createImage(Display.getInstance().getResourceAsStream(null, imageName));
        return image;
    } catch (IOException ioe) {
        //Log.p("Image " + name + " not found: " + ioe);
    }
    return null;
}

2:

Add the images through the GUI Builder (theme.res) and get them as follows:

public static Image getImageFromTheme(String imageName) {
    try {
        Resources resFile = Resources.openLayered("/theme");//Change 'theme' to the custom name, if you renamed your theme.res file
        Image image = resFile.getImage(imageName);
        return image;
    } catch (IOException ioe) {
        //Log.p("Image " + name + " not found: " + ioe);
    }
    return null;
}
Diamond
  • 7,428
  • 22
  • 37
1

You have to use Like

UIImageView *yourImageView; // this must be your IBOutlet imageview, else you have to allocate it manually by giving its size frame etc.
[yourImageView setImage:[UIImage imageNamed:@"xxx.png"]];

Try this, it will work. Thanks.

Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46
  • tried this way it was giving exception solved by [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xxx.png"]]; but still didn't solve this issue – AhMaD AbUIeSa Mar 16 '17 at 07:15
  • What you actually wanted do here..? please provide some codes. – Abhishek Mitra Mar 16 '17 at 08:32
  • I am using codename one native code to create video chat , and while doing so , I am using the sdk that have set of pics with some design that I want to include to my application , the problem is when I add the images the generated source code that codename one provide , is not added , I am trying to figure out a way to include and use them from the code – AhMaD AbUIeSa Mar 16 '17 at 09:10