游戏过程中可能需要实现这样的功能:一张很漂亮的壁纸,开发者们可能允许玩家下载到本地,那么,代码中
应该如何实现呢?答案是
混编。
此后,再新建一个SaveImageControl的类
.mm文件
使用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