加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx lua ios glview 截图

发布时间:2020-12-14 21:20:31 所属栏目:百科 来源:网络整理
导读:-(NSString ) grabScreenShots:(NSDictionary )argsDic { NSString *fileN = @"";NSString* fileName = [argsDic objectForKey:@"fileName"];float sysVersion = [[[UIDevice currentDevice] systemVersion] floatValue];if (sysVersion 7.0) { CGFloat scal

-(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;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读