0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cocos2d-x imagepicker - 사진 앨범 접근하기

Last updated at Posted at 2019-06-29

cocos2d-x 3.17.1
AndroidStudio 3.4.1
github source -> imagepicker


  • 소스 경로

소스를 다운로드해서 ImagePicker폴더를 프로젝트의 아래 경로에 넣습니다.
cocos2d/extensions/ImagePicker


  • 소스 포함 시키기

CMakeLists.txt 수정

CMakeLists.txt
# add cross-platforms source files and header files 
list(APPEND GAME_SOURCE
     cocos2d/extensions/ImagePicker/ImagePicker.cpp
     cocos2d/extensions/ImagePicker/ImagePickerImpl.cpp
)

list(APPEND GAME_HEADER
cocos2d/extensions/ImagePicker/ImagePicker.h
cocos2d/extensions/ImagePicker/ImagePickerImpl.h
)

  • 프로젝트 소스에서 include
include.h
#include "ImagePicker/ImagePicker.h" 

  • 적용 예

샘플 소스의 HelloWorld.cpp를 참고하여 만들었습니다.

PhotoOpener.h
#ifndef __PHOTHOPENER_SCENE_H__
#define __PHOTHOPENER_SCENE_H__

#include "cocos2d.h"

class PhotoOpener : public cocos2d::Layer, public cocos2d::ImagePickerDelegate
{
public:
    static cocos2d::Scene* createScene();
    
    virtual bool init();
    
    void didFinishPickingWithResult(cocos2d::Texture2D* result);    
    
    // implement the "static create()" method manually
    CREATE_FUNC(PhotoOpener);
};

#endif // __PhotoOpener_SCENE_H__
PhotoOpener.cpp
bool PhotoOpener::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    ImagePicker::getInstance()->pickImage(this);  
    return true;
}

// call back
void PhotoOpener::didFinishPickingWithResult(cocos2d::Texture2D* result)
{
    if(nullptr == result)
    {
        log("%s | no photo", __FUNCTION__);
        Director::getInstance()->popScene();
        return;
    }
    
    //
    // 540 x 960
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    //
    // anchor 0.5, 0.5
    auto sprite = Sprite::createWithTexture(result);
    
    //
    // sprite size
    Size sprSize = sprite->getContentSize();
    
    //
    // pos
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));    
    
    //
    // scale
    float scalfator = visibleSize.width/sprSize.width;
    sprite->setScale(scalfator);
    
    //
    // to do..
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?