您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: