/** * Returns a Texture2D object given an CGImageRef image If the image was not previously loaded, it * will create a new CCTexture2D object and it will return it. Otherwise it will return a * reference of a previously loaded image The "key" parameter will be used as the "key" for the * cache. If "key" is nil, then a new texture will be created each time. * * <p>BE AWARE OF the fact that copy of image is stored in memory, use assets method if you can. * * @since v0.8 */ public CCTexture2D addImage(Bitmap image, String key) { assert (image != null) : "TextureCache: image must not be null"; CCTexture2D tex = null; if (key != null && (tex = textures.get(key)) != null) { return tex; } final Bitmap copy = image.copy(image.getConfig(), false); if (copy != null) { final CCTexture2D texNew = new CCTexture2D(); texNew.setLoader( new GLResourceHelper.GLResourceLoader() { @Override public void load() { Bitmap initImage = copy.copy(copy.getConfig(), false); texNew.initWithImage(initImage); } }); if (key != null) { textures.put(key, texNew); } return texNew; } else { ccMacros.CCLOG("cocos2d", "Couldn't add Bitmap in CCTextureCache"); return null; } }
private static CCTexture2D createTextureFromFilePathExternal(final String path) { final CCTexture2D tex = new CCTexture2D(); tex.setLoader( new GLResourceHelper.GLResourceLoader() { @Override public void load() { try { InputStream is = new FileInputStream(path); Bitmap bmp = BitmapFactory.decodeStream(is); is.close(); tex.initWithImage(bmp); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); return tex; }