3

我想截屏并将其保存在图库中。我试过的:

CCSize size = CCDirector::sharedDirector()->getWinSize();

CCRenderTexture* texture = CCRenderTexture::create((int)size.width, (int)size.height, kCCTexture2DPixelFormat_RGBA8888);
texture->setPosition(ccp(size.width/2, size.height/2));
texture->begin();
CCDirector::sharedDirector()->getRunningScene()->visit();
texture->end();
texture->saveToFile("scrshot.png", kCCImageFormatJPEG);

在 iOS 中它运行良好并且能够将图像保存到文档中,但问题出在 android 中:

有两种可能

1) 纹理->saveToFile("scrshot.png", kCCImageFormatJPEG); 或纹理->saveToFile("我的路径"); -> 它编译但图像保存在哪里?

2)使用JNI保存Bitmap文件->问题是如何将CCRenderTexture转换为Bitmap并解析它。

4

1 回答 1

4

我很快解决了这个问题,但忘了给出答案,但现在给出,因为它可能对某人有所帮助

我的代码仅适用于 iOS 和 android

代码是

CCSize size = CCDirector::sharedDirector()->getWinSize();

CCRenderTexture* texture = CCRenderTexture::create((int)size.width, (int)size.height, kCCTexture2DPixelFormat_RGBA8888);
texture->setPosition(ccp(size.width/2, size.height/2));
texture->begin();
CCDirector::sharedDirector()->getRunningScene()->visit();
texture->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

    std::string str =  CCFileUtils::sharedFileUtils()->getWritablePath();
    str.append("/imageNameToSave.png");
    const char * c = str.c_str();
    texture->saveToFile(c);
    SaveImageAndroidJNI(true);

#else

        texture->saveToFile("imageNameToSave.png", kCCImageFormatPNG);
        BridgeClass::shared()->saveTOAlbum();

#endif

在我的 BridgeClass 中

void BridgeClass:: saveTOAlbum(){

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *yourArtPath = [documentsDirectory stringByAppendingPathComponent:@"/imageNameToSave.png"];

    UIImage *image = [UIImage imageWithContentsOfFile:yourArtPath];

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert !" message:@"Photo Saved To Photos." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

对于 iOS,它将保存到图库

对于 android 有一个类 androidJNI.cpp

void SaveImageAndroidJNI(bool visible){
        JniMethodInfo t;
        if (JniHelper::getStaticMethodInfo(t, "yourPackageName/ClassName"
                                           ,"SaveImageAndroidJNI"
                                           ,"(Z)V"))
        {
            t.env->CallStaticVoidMethod(t.classID,t.methodID,visible);
        }
    }

以及将图像保存到数据/数据的android本机方法

static void SaveImageAndroidJNI(final boolean visible)
{

    ContextWrapper c = new ContextWrapper(me);
    String path = c.getFilesDir().getPath() + "/imageNameToSave.png";
    System.out.println("Paht to check --"+path);
    File imgFile = new  File(path);

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    ArrayList<Uri> uris = new ArrayList<Uri>();
    uris.add(Uri.parse(path));

    OutputStream output;
 // Find the SD Card path
    File filepath = Environment.getExternalStorageDirectory();

    // Create a new folder in SD Card
    File dir = new File(filepath.getAbsolutePath()
            + "/Your Folder Name/");
    dir.mkdirs();

    // Create a name for the saved image
    File file = new File(dir, "imageNameToSave.png");

    try {

        output = new FileOutputStream(file);

        // Compress into png format image from 0% - 100%
        myBitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
        output.flush();
        output.close();
    }

    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    Intent intent = new Intent();
    Uri pngUri = Uri.fromFile(file);


    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM, pngUri);
    intent.setType("image/jpeg");

        me.startActivity(Intent.createChooser(intent, "Share Image"));    
}

当从数据/数据访问此图像时,此处无法直接访问将此图像保存到 sd 卡并从 cocos2dx 访问

但是对于 android 必须使用 getWritablePath 函数获取可写路径并将此值解析为 saveToFile 函数并且SaveImageAdmobJNI(true);将调用本机方法,在本机方法中您可以保存获取图像路径并将其保存到画廊

注意: getWritablePath() - 这个函数会给你从 data->data->YOUR_PAKAGE 的路径

随时提出疑问。

于 2014-09-02T06:01:09.883 回答