cocos2dx lua ios glview 截图

-(Nsstring ) grabScreenShots:(NSDictionary )argsDic {

Nsstring *fileN = @"";
Nsstring* fileName = [argsDic objectForKey:@"fileName"];
float sysversion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (sysversion < 7.0) {
    CGFloat scale = [[UIScreen mainScreen] scale];
    int w = self.view.bounds.size.width*scale;
    int h = self.view.bounds.size.height*scale;
    NSInteger myDataLength = w * h * 4;

    // allocate array and read pixels into it.
    glubyte *buffer = (glubyte *) malloc(myDataLength);
    glreadPixels(0,w,h,GL_RGBA,GL_UNSIGNED_BYTE,buffer);

    // gl renders "upside down" so swap top to bottom into new array.
    // there's gotta be a better way,but this works.
    glubyte *buffer2 = (glubyte *) malloc(myDataLength);
    for(int y = 0; y < h; y++) {
        for(int x = 0; x <w * 4; x++) {
            buffer2[(h -1 - y) * w * 4 + x] = buffer[y * 4 * w + x];
        }
    }

    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,buffer2,myDataLength,NULL);

    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * w;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    // make the cgimage
    CGImageRef imageRef = CGImageCreate(w,bitsPerComponent,bitsPerPixel,bytesPerRow,colorSpaceRef,bitmapInfo,provider,NULL,NO,renderingIntent);

    // then make the uiimage from that
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    NSData * data = UIImagePNGRepresentation(myImage);

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    fileN = [[path objectAtIndex:0] stringByAppendingPathComponent:fileName];
    NSLog(@"filename is %@",fileN);
    [data writetoFile:fileN atomically:YES];

    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpaceRef);
    free(buffer);
} else {
    CGSize size = self.view.bounds.size;

    UIGraphicsBeginImageContextWithOptions(size,[UIScreen mainScreen].scale);

    CGRect rec = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,self.view.bounds.size.width,self.view.bounds.size.height);
    [self.view drawViewHierarchyInRect:rec afterScreenUpdates:NO];

    UIImage *image = UIGraphicsGetimageFromCurrentimageContext();
    UIGraphicsEndImageContext();

    NSData * data = UIImagePNGRepresentation(image);

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,fileN);
    [data writetoFile:fileN atomically:YES];

// UIImageWritetoSavedPhotosAlbum(image,nil,nil);
}

return fileN;

}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...