Tag Archives: iOS

mac下编译运行Cocos2d-x 2.0.4 android

由于从2.0.3开始,cocos2d-x把libcocos2dx从每个android项目中单独挪了出来,放在了{你的cocos2d-x源代码位置}/cocos2dx/platform/android/java,所以在eclipse中导入android项目后,都需要import这个位置的lib项目进来,不然都会提示找不到libcocos2dx

导入libcocos2dx如果报错,如下图

libcocos2dx需要JRE 1.6版本,mac默认就有,我是lion系统,终端输入java -version,提示1.6版本

右击libcocos2dx,选择属性,从java compiler选项卡里选择compilance level为1.6,然后就无错可以继续编译运行了

Tagged ,

ShareKit设置Localize无效的解决方法

由于新App需要分享到各大网站,所以用了ShareKit这个开源库。为了广大中文用户,所以要对菜单名称等进行本地化语言处理一下。新版本的ShareKit自带了Localize的string文件,本以为可以直接使用,结果无论是模拟器还是真机测试,中文信息都无效…

查了好久完全不知道怎么回事

解决方法很简单

删掉自带的Localizable.strings文件,然后重新自己一一建立,立刻就解决

具体原因不解,但是删掉自己重来很管用

Tagged

Cocos2D中实现截图功能

在Cocos2D中截图有2种方法

  • 从OpenGL的显示数据中直接获取,这样可以截取整个屏幕显示的结果,类似Home+Power的效果
  • 使用CCRenderTexture可以单独设定特定的Sprite或者CCNode进行截取保存

OpenGL截取整个屏幕

需要对CCDirector进行Category扩展,加入以下两个文件

CCDirector+ImageSave.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface CCDirector (ImageSave)

- (UIImage *)screenShotUIImage;
- (CCTexture2D *)screenShotTexture2D;

@end

CCDirector+ImageSave.m

#import "CCDirector+ImageSave.h"

@implementation CCDirector (ImageSave)

- (UIImage*) screenShotUIImage {
CGSize size = [self displaySizeInPixels];
//Create un buffer for pixels
GLuint bufferLenght=size.width*size.height*4;
GLubyte *buffer = (GLubyte *) malloc(bufferLenght);

//Read Pixels from OpenGL
glReadPixels(0,0,size.width,size.height,GL_RGBA,GL_UNSIGNED_BYTE,buffer);
//Make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLenght, NULL);

//Configure image
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * size.width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate(size.width,size.height,bitsPerComponent,bitsPerPixel,bytesPerRow,colorSpaceRef,bitmapInfo,provider,NULL,NO,renderingIntent);

uint32_t *pixels = (uint32_t *)malloc(bufferLenght);
CGContextRef context = CGBitmapContextCreate(pixels, [self winSize].width, [self winSize].height, 8, [self winSize].width*4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGContextTranslateCTM(context,0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);

switch ([self deviceOrientation]) {
case CCDeviceOrientationPortrait:
break;
case CCDeviceOrientationPortraitUpsideDown:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180));
CGContextTranslateCTM(context,-size.width, -size.height);
break;
case CCDeviceOrientationLandscapeLeft:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));
CGContextTranslateCTM(context,-size.height, 0);
break;
case CCDeviceOrientationLandscapeRight:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90));
CGContextTranslateCTM(context,size.width*0.5, -size.height);
break;
}
CGContextDrawImage(context, CGRectMake(0.0, 0.0, size.width, size.height), iref);
UIImage *outputImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];

//Dealloc
CGDataProviderRelease(provider);
CGImageRelease(iref);
CGContextRelease(context);
free(buffer);
free(pixels);

return outputImage;
}

- (CCTexture2D*) screenShotTexture2D {
return [[CCTexture2D alloc] initWithImage:[self screenShotUIImage]];
}

@end

使用的时候,如下就可以保存图片

#import "CCDirector+ImageSave.h"

/*
...
...
*/

UIImage *screenshot = [[CCDirector sharedDirector] screenShotUIImage];
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Screenshot.png"];
[UIImagePNGRepresentation(screenshot) writeToFile:savePath atomically:YES];

CCRenderTexture截图部分CCNode图象

CCRenderTexture是Cocos2D定义的类,所以直接使用吧

// 设定截图大小
CCRenderTexture  *target = [CCRenderTexture renderTextureWithWidth:320 height:480];
[target begin];

// 添加需要截取的CCNode
[someNode visit];

[target end];
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Screenshot.png"];
[target saveBuffer:savePath format:kCCImageFormatPNG];
Tagged ,

Core Data下如何分页查询数据

其实分页查询我第一反映是用SQLite的查询语句来操作,后来想想Core Data底层也是调用SQLite,应该也是可以分页查询的
不过iPhone开发毕竟中文资料不多,翻了下官方英文文档,找到下面的方法,主要使用下面两个函数

// 限定查询结果的数量
setFetchLimit
// 查询的偏移量
setFetchOffset

最近正在熟悉各种API,没太多时间,直接上代码,其实蛮简单的

	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	NSEntityDescription *entity = [NSEntityDescription entityForName:@"RSSEntryModel" inManagedObjectContext:_managedObjectContext];
	[request setEntity:entity];
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"articleDate" ascending:NO];
	NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
	[request setSortDescriptors:sortDescriptors];
	[request setFetchLimit:21];
	[request setFetchOffset:_currentPage * 21];
	NSArray *rssTemp = [_managedObjectContext executeFetchRequest:request error:&error];

这里贴上一个教程http://www.raywenderlich.com/934/core-data-tutorial-getting-started,3个篇幅说得更加仔细
他blog里的教程都很棒,我基本都看了遍

Tagged ,