ios的一些平台相关代码
2012-04-06 14:15
501 查看
《捕鱼之海底捞》的IOS版已经在APP STORE上线近一个月,表现还算不错。使用了cocos2dx引擎,C++语言开发。这里总结一下用到的一些平台相关的代码。代码大多来自网上,尤其感谢stackoverflow。
多点触摸
[__glView
setMultipleTouchEnabled:YES];
报告内存状态
-(void) report_memory {
struct
task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory in use (in bytes): %u", info.resident_size);
}
else {
NSLog(@"Error with task_info(): %s",
mach_error_string(kerr));
}
}
阻止设备进入待机状态
[[UIApplication
sharedApplication] setIdleTimerDisabled:YES];
弹出提示框
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:@"title" message:@"message" delegate:self
cancelButtonTitle:@"ok"otherButtonTitles:nil];
[alert
show];
[alert
release];
c++与oc字符串互转
NSString *ocs = [NSString stringWithUTF8String:"test"];
std::string cs = [ocs UTF8String];
检查络是否连接
bool connectedToNetwork(){
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len =
sizeof(zeroAddress);
zeroAddress.sin_family =
AF_INET;
SCNetworkReachabilityRef defaultRouteReachability =
SCNetworkReachabilityCreateWithAddress(NULL, (struct
sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
printf("Error. Could not recover network reachability flags\n");
return false;
}
BOOL isReachable = ((flags & kSCNetworkFlagsReachable) !=
0);
BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) !=
0);
return (isReachable && !needsConnection) ? true :
false;
}
跳转到APP STORE进行评分(打开网页,邮箱类似)
[[UIApplication
sharedApplication]
openURL:[NSURL
URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=500619796"]];
开启新线程
+(void) newThread{
[NSThread
detachNewThreadSelector:@selector(startNewThread)
toTarget:self
withObject:nil];
}
+(void) startNewThread{
//do something
}
OPENGL截屏
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
#define AWIMAGE UIImage*
#else
#define AWIMAGE CGImageRef
#endif
@implementation ScreenShotIos
+(CGImageRef) takeAsCGImage{
CCDirector *director = CCDirector::sharedDirector();
CCSize displaySize = director->getDisplaySizeInPixels();
CCSize winSize = director->getWinSizeInPixels();
GLuint bufferLength = displaySize.width * displaySize.height *
4;
GLubyte *buffer = (GLubyte *)malloc(bufferLength);
glReadPixels(0,
0, displaySize.width, displaySize.height,
GL_RGBA, GL_UNSIGNED_BYTE, buffer);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength,
NULL);
CGColorSpaceRef colorSpaceRef =
CGColorSpaceCreateDeviceRGB();
CGImageRef iref = CGImageCreate(displaySize.width, displaySize.height,
8, 32, displaySize.width *
4, colorSpaceRef,
kCGBitmapByteOrderDefault, provider, NULL,
NO, kCGRenderingIntentDefault);
uint32_t* pixels = (uint32_t*)malloc(winSize.width * winSize.height *
4);
CGContextRef context = CGBitmapContextCreate(pixels, winSize.width, winSize.height,
8, winSize.width *
4, colorSpaceRef,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
CGContextTranslateCTM(context, 0, displaySize.height);
CGContextScaleCTM(context, 1, -1);
switch (director->getDeviceOrientation()) {
case
kCCDeviceOrientationPortrait:{
break;
}
case
kCCDeviceOrientationPortraitUpsideDown:{
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180));
CGContextTranslateCTM(context, -displaySize.width, -displaySize.height);
break;
}
case
kCCDeviceOrientationLandscapeLeft:{
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));
CGContextTranslateCTM(context, -displaySize.height,
0);
break;
}
case
kCCDeviceOrientationLandscapeRight:{
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90));
CGContextTranslateCTM(context, displaySize.height - displaySize.width, -displaySize.height);
break;
}
default:{
break;
}
}
CGContextDrawImage(context, CGRectMake(0.0f,
0.0f, displaySize.width, displaySize.height), iref);
#else
CGContextTranslateCTM(context,
0, winSize.height);
CGContextScaleCTM(context,
1, -1);
CGContextDrawImage(context, CGRectMake(0.0f,
0.0f, winSize.width, winSize.height), iref);
#endif
CGImageRef imageRef = CGBitmapContextCreateImage(context);
CGDataProviderRelease(provider);
CGImageRelease(iref);
CGColorSpaceRelease(colorSpaceRef);
CGContextRelease(context);
free(buffer);
free(pixels);
return imageRef;
}
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
+ (UIImage*) takeAsImage{
CGImageRef imageRef = [self
takeAsCGImage];
UIImage *outImage = [[UIImage
alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);
return outImage;
}
#else
+ (CGImageRef) takeAsImage{
return [self takeAsCGImage];
}
#endif
+(void) saveImage:(UIImage *) img{
UIImageWriteToSavedPhotosAlbum(img, nil,
nil,
nil);
}
@end
多点触摸
[__glView
setMultipleTouchEnabled:YES];
报告内存状态
-(void) report_memory {
struct
task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory in use (in bytes): %u", info.resident_size);
}
else {
NSLog(@"Error with task_info(): %s",
mach_error_string(kerr));
}
}
阻止设备进入待机状态
[[UIApplication
sharedApplication] setIdleTimerDisabled:YES];
弹出提示框
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:@"title" message:@"message" delegate:self
cancelButtonTitle:@"ok"otherButtonTitles:nil];
[alert
show];
[alert
release];
c++与oc字符串互转
NSString *ocs = [NSString stringWithUTF8String:"test"];
std::string cs = [ocs UTF8String];
检查络是否连接
bool connectedToNetwork(){
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len =
sizeof(zeroAddress);
zeroAddress.sin_family =
AF_INET;
SCNetworkReachabilityRef defaultRouteReachability =
SCNetworkReachabilityCreateWithAddress(NULL, (struct
sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
printf("Error. Could not recover network reachability flags\n");
return false;
}
BOOL isReachable = ((flags & kSCNetworkFlagsReachable) !=
0);
BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) !=
0);
return (isReachable && !needsConnection) ? true :
false;
}
跳转到APP STORE进行评分(打开网页,邮箱类似)
[[UIApplication
sharedApplication]
openURL:[NSURL
URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=500619796"]];
开启新线程
+(void) newThread{
[NSThread
detachNewThreadSelector:@selector(startNewThread)
toTarget:self
withObject:nil];
}
+(void) startNewThread{
//do something
}
OPENGL截屏
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
#define AWIMAGE UIImage*
#else
#define AWIMAGE CGImageRef
#endif
@implementation ScreenShotIos
+(CGImageRef) takeAsCGImage{
CCDirector *director = CCDirector::sharedDirector();
CCSize displaySize = director->getDisplaySizeInPixels();
CCSize winSize = director->getWinSizeInPixels();
GLuint bufferLength = displaySize.width * displaySize.height *
4;
GLubyte *buffer = (GLubyte *)malloc(bufferLength);
glReadPixels(0,
0, displaySize.width, displaySize.height,
GL_RGBA, GL_UNSIGNED_BYTE, buffer);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength,
NULL);
CGColorSpaceRef colorSpaceRef =
CGColorSpaceCreateDeviceRGB();
CGImageRef iref = CGImageCreate(displaySize.width, displaySize.height,
8, 32, displaySize.width *
4, colorSpaceRef,
kCGBitmapByteOrderDefault, provider, NULL,
NO, kCGRenderingIntentDefault);
uint32_t* pixels = (uint32_t*)malloc(winSize.width * winSize.height *
4);
CGContextRef context = CGBitmapContextCreate(pixels, winSize.width, winSize.height,
8, winSize.width *
4, colorSpaceRef,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
CGContextTranslateCTM(context, 0, displaySize.height);
CGContextScaleCTM(context, 1, -1);
switch (director->getDeviceOrientation()) {
case
kCCDeviceOrientationPortrait:{
break;
}
case
kCCDeviceOrientationPortraitUpsideDown:{
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180));
CGContextTranslateCTM(context, -displaySize.width, -displaySize.height);
break;
}
case
kCCDeviceOrientationLandscapeLeft:{
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));
CGContextTranslateCTM(context, -displaySize.height,
0);
break;
}
case
kCCDeviceOrientationLandscapeRight:{
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90));
CGContextTranslateCTM(context, displaySize.height - displaySize.width, -displaySize.height);
break;
}
default:{
break;
}
}
CGContextDrawImage(context, CGRectMake(0.0f,
0.0f, displaySize.width, displaySize.height), iref);
#else
CGContextTranslateCTM(context,
0, winSize.height);
CGContextScaleCTM(context,
1, -1);
CGContextDrawImage(context, CGRectMake(0.0f,
0.0f, winSize.width, winSize.height), iref);
#endif
CGImageRef imageRef = CGBitmapContextCreateImage(context);
CGDataProviderRelease(provider);
CGImageRelease(iref);
CGColorSpaceRelease(colorSpaceRef);
CGContextRelease(context);
free(buffer);
free(pixels);
return imageRef;
}
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
+ (UIImage*) takeAsImage{
CGImageRef imageRef = [self
takeAsCGImage];
UIImage *outImage = [[UIImage
alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);
return outImage;
}
#else
+ (CGImageRef) takeAsImage{
return [self takeAsCGImage];
}
#endif
+(void) saveImage:(UIImage *) img{
UIImageWriteToSavedPhotosAlbum(img, nil,
nil,
nil);
}
@end
相关文章推荐
- IOS中用户界面及一些基本按钮相关代码实例
- Cocos2dx IOS平台相关代码
- Unity调用Android与IOS平台代码
- iOS常用的一些正则表达式代码举例
- iOS_ UITextField相关代码
- IOS android平台上都可使用的js分享代码
- 查找rust平台相关代码的文档
- Cocos2d-x游戏开发之代码如何识别ios平台与android并分开处理
- Cocos2d-x 关于在iOS平台真机测试的一些注意
- 通过ios实现RSA加密解密中的 NSString+Base64.h/m相关代码
- iOS代码重构的一些方法
- iOS开发之一些字符串常用的代码
- 总结一些iOS项目中组织代码的方法
- iOS开源代码 —— UI相关
- iOS:苹果推送(三)----一些示例代码和注意的问题
- iOS-上传视频到服务器的一些相关处理
- iOS高德地图的相关配置和简单演示代码
- Unity发布到iOS平台后的相关问题
- 与Pocket Outlook相关的一些代码片段(C#)
- 通过ios实现RSA加密解密中的 RSAEncryptor.h/m相关代码