Cocos2dx-截屏並設置圖片尺寸
阿新 • • 發佈:2019-03-15
sse eas sel 地址 visit nim 當前 nsa search
猴子原創,歡迎轉載。轉載請註明: 轉載自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); }
?
?
Cocos2dx-截屏並設置圖片尺寸