[iuud8]基于cocos2dx2.26下载项目中的图片到本地(ios版)

游戏过程中可能需要实现这样的功能:一张很漂亮的壁纸,开发者们可能允许玩家下载到本地,那么,代码中 应该如何实现呢?答案是 混编。

使用c++代码调用oc代码,使用ios自己的库即可。代码如下:

很简单,就不加注释了。

首先新建一个SaveImagesToIPhoneTool的类

.h文件

#include <cocos2d.h>

class SaveImagesToIPhoneTool
{
public:
    static SaveImagesToIPhoneTool *getInstance();
    
    void saveImageToFiles(const char *imagePath);
};
.mm文件

此处注意,之所以取文件的完整路径,是因为UIImage需要图片的完整路径才能被创建

#include "SaveImagesToIPhoneTool.h"
#include "SaveImageController.h"

static SaveImagesToIPhoneTool *m_saveImages = NULL;

SaveImagesToIPhoneTool* SaveImagesToIPhoneTool::getInstance()
{
    if (m_saveImages == NULL)
    {
        m_saveImages = new SaveImagesToIPhoneTool();
    }
    
    return m_saveImages;
}

void SaveImagesToIPhoneTool::saveImageToFiles(const char *imagePath)
{
    std:: string fullPath = cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(imagePath);
    
    cocos2d::CCLog(">>>%s", fullPath.c_str());

    [[SaveImagesController getInsTance] saveImage:[[NSString alloc] initWithString:[NSString stringWithFormat:@"%s", fullPath.c_str()]]];
}

此后,再新建一个SaveImageControl的类

.h文件

#import <Foundation/Foundation.h>

@interface SaveImagesController:NSObject{

};

+(SaveImagesController *)getInsTance;

-(void)saveImage:(NSString *)filePath;

@end

.mm文件

#import "SaveImageController.h"

static SaveImagesController* m_SaveImagesControler=nil;

@implementation SaveImagesController

+(SaveImagesController *)getInsTance
{
    if(m_SaveImagesControler==nil)
    {
        m_SaveImagesControler=[[SaveImagesController alloc]init];
    }
    
    return m_SaveImagesControler;
}

-(void)saveImage:(NSString *)filePath
{
    NSLog(@"%@", filePath);
    NSLog(@">>>>>%@", NSHomeDirectory());
    UIImage *myImage = [UIImage imageWithContentsOfFile:filePath];
    if(myImage)
    {
        UIImageWriteToSavedPhotosAlbum(myImage, self, @selector(savingImageIsFinished:didFinishSavingWithError:contextInfo:), nil);
    }
}

//  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
-(void) savingImageIsFinished:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        
        NSLog(@"%@", error);
        UIAlertView *alert
        = [[[UIAlertView alloc] initWithTitle:@"保存失败"
                                      message:@"请查看设置->隐私->照片中的APPName是否被允许存储照片。" delegate:nil
                            cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
        [alert show];
        
    } else {
        UIAlertView *alert
        = [[[UIAlertView alloc] initWithTitle:@"保存成功"
                                      message:@"请在相册中查看保存的图片。" delegate:nil
                            cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
        [alert show];
        
    }
}

@end


以上


SaveImagesController

SaveImagesController

SaveImagesController

SaveImagesController

SaveImagesController


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值