• Cocos2dx-截屏并设置图片尺寸


    猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢!

    原文地址: http://www.cocos2dev.com/?p=522

    前几天添加微信图片分享的时候,发现全屏截图超出了微信的数据包大小,所以截屏的时候可以考虑缩小尺寸到0.5倍。
    下面的截屏代码:

        void LHUtil::screenShoot()  
        {  
            Size visibleSize = Director::getInstance()->getVisibleSize();  
              
            //定义一个屏幕大小的渲染纹理  
            RenderTexture* renderTexture = RenderTexture::create(visibleSize.width * .5, visibleSize.height * .5, Texture2D::PixelFormat::RGBA8888);  
          
            Scene* curScene = Director::getInstance()->getRunningScene();  
            Point ancPos = pCurScene->getAnchorPoint();  
          
            //渲染纹理开始捕捉  
            renderTexture->begin();  
          
            // 缩小屏幕截屏区域  
            curScene->setScale(.5);  
            curScene->setAnchorPoint(cocos2d::Point(0, 0));  
          
            //绘制当前场景  
            curScene->visit();  
          
            //结束  
            renderTexture->end();  
          
            //保存png  
            renderTexture->saveToFile("screenshoot.png", Image::Format::PNG);  
          
            // 恢复屏幕尺寸  
            curScene->setScale(1);  
            curScene->setAnchorPoint(ancPos);  
            CC_SAFE_DELETE(curScene);  
        }  

    上面是cocos2dx的获取截屏的方法。

    我顺便写下如何用OC的UIGraphicsBeginImageContext获取UIView转化成UIImage.
    下面是ios方法:


        -(void)screenShot:(CGRect)rect{  
            // 开始设置截屏区域  
            UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0);  
            [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];  
            UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();  
            UIGraphicsEndImageContext();  
              
            // 获取image,可以根据需要进行尺寸修改  
            CGImageRef imageRef = viewImage.CGImage;  
            CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);  
            UIImage *image = [[UIImage alloc] initWithCGImage:imageRefRect];  
              
            // 保存图片到相册, 这里会提示用户授权,不需要保存的话,可以取消  
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);  
              
            // 获取Documents目录  
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
            NSString *documentsDirectory = [paths objectAtIndex:0];  
            NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"temScreenShot.png"];  
              
            // 保存image到Documents目录  
            NSData *imageData = UIImagePNGRepresentation(image);  
            [imageData writeToFile:savedImagePath atomically:YES];  
            CGImageRelease(imageRefRect);  
        }  





  • 相关阅读:
    vs2005设置断点不能调试问题(方法三为首选项,一般都可以解决)
    SQL中内连接和外连接的问题!
    javascript读写删cookie的简单方法
    数据库语句 select * from table where 1=1 的用法和作用
    gridview 和repeater 添加序号的方法
    asp.net Forms身份验证详解(转载)
    Asp.net中的认证与授权(转载)
    ASP.NET中前台javascript与后台代码调用
    android 模拟器不能上网解决方法
    大数据量系统架构
  • 原文地址:https://www.cnblogs.com/shiweihappy/p/4246468.html
Copyright © 2020-2023  润新知