I made a Pull Request before and seems cocos doesn't need it :) So I just put all codes along with test here.
ImagePicker utility for cocos2d-x v3
Attention
This utility lets you load images from device gallery and load it to your application as a Texture2D. You may draw Sprite with this texture or use it in other ways.
The origin project is https://github.com/Losiowaty/ImagePicker, and I clear the whole project, in addition to:
The image is loaded by platform API and deliveried to cocos in memory directly. No repeated IO is needed.
usage example screenshots: http://qiankanglai.me/project/2014/04/14/cocos2dx-ImagePicker/
Add the following sources into project according to different platforms (Files arranged with default cocos project folder structure). You need to add following files to xcodeproj/Android.mk/vcxproj.
cocos2dx
proj.win8.1-universal
JSB
Tests (optional)
Example Usage
Just call ImagePicker::getInstance()->pickImage()
and pass in your delegate. If the image is picked successfully, your delegate will receive the corresponding Texture2D, or nullptr otherwise. An example could be found in HelloWorldScene.
Attention Please don't try get the texture immediately after pickImage
. You have to wait for the system and this is why I'm using delegate!
For cocos2d-JS users: add sc->addRegisterCallback(register_all_cocos2dx_extension_ImagePicker);
in AppDelegate.cpp,
var delegator = new jsb.ImagePickerDelegate();
delegator.didFinishPickingWithResult = function(texture){
if(texture) {
var sprite = cc.Sprite.createWithTexture(texture);
var winSize = cc.director.getWinSize();
sprite.x = winSize.width / 2;
sprite.y = winSize.height / 2;
self.addChild(sprite);
}
else {
cc.log("ImagePicker return null");
}
};
var picker = jsb.ImagePicker.getInstance();
picker.pickImage(delegator);