iOS實現截圖 併合適儲存
阿新 • • 發佈:2019-01-10
開發遊戲時,往往會有這麼一個需求:在某個成就達成或者破紀錄時,需要截個屏,然後傳送到微博上與好友/粉絲分享,雖然home + 開機鍵組合可手動截圖,在Cocos2d有個CCRenderTexture類,藉助該類可很容易實現程式碼擷取功能。使用CCRenderTexture,我們可以擷取遊戲場景、某個Layer,甚至是精靈:
- /**遊戲截圖
- *@param node 需要擷取的控制元件
- */
- - (void)snapshotScreen:(CCNode*)node
- {
- //取得螢幕大小
-
CGSize winSize = [[CCDirector sharedDirector]winSize];
- CCRenderTexture* renderTexture = [CCRenderTexture renderTextureWithWidth:winSize.width
- height:winSize.height];
- [renderTexture begin];
- [node visit];
- [renderTexture end];
-
[renderTexture cleanup];
- UIImage *snapshot = [renderTexture getUIImageFromBuffer];
- //把截圖儲存到相簿裡
- UIImageWriteToSavedPhotosAlbum(snapshot, nil, nil, nil);
- }
如果遊戲支援高清模式,上面程式碼截出來的圖是960 * 640大小的,這個尺寸如果要上傳到微博上,檔案的大小可能會超出限制,那麼在上傳前可用先把截圖大小縮小點如480*320,以減少圖片體積:
- /** 調整圖片大小*/
-
- (UIImage *) scaleFromImage: (UIImage *) image toSize: (CGSize) size
- {
- UIGraphicsBeginImageContext(size);
- [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
同時可以儲存
- 儲存檔案
- UIImage *m_imgFore=......;
- //png格式
- NSData *imagedata=UIImagePNGRepresentation(m_imgFore);
- //JEPG格式
- //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
- NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
- NSString *documentsDirectory=[paths objectAtIndex:0];
- NSString *savedImagePath=[documentsDirectorystringByAppendingPathComponent:@"saveFore.png"];
- [imagedata writeToFile:savedImagePath atomically:YES];
- 或者
- [fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.png"] contents:data attributes:nil]; 將圖片儲存為PNG格式
- [fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.jpg"] contents:data attributes:nil]; 將圖片儲存為JPEG格式
如果不是cocos2D開發,則使用如下程式碼:
方法1:
- -(void)screenShots
- {
- CGSize imageSize = [[UIScreen mainScreen] bounds].size;
- if (NULL != UIGraphicsBeginImageContextWithOptions) {
- UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
- }
- else
- {
- UIGraphicsBeginImageContext(imageSize);
- }
- CGContextRef context = UIGraphicsGetCurrentContext();
- for (UIWindow * window in [[UIApplication sharedApplication] windows]) {
- if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
- CGContextSaveGState(context);
- CGContextTranslateCTM(context, [window center].x, [window center].y);
- CGContextConcatCTM(context, [window transform]);
- CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y);
- [[window layer] renderInContext:context];
- CGContextRestoreGState(context);
- }
- }
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
- NSLog(@"Suceeded!");
- }
所以再提供另外一方法:
方法2
- UIView *view = [[[[[UIApplication sharedApplication] windows] objectAtIndex:1] subviews] lastObject];//獲得某個window的某個subView
- NSInteger index = 0;//用來給儲存的png命名
- for (UIView *subView in [view subviews]) {//遍歷這個view的subViews
- if ([subView isKindOfClass:NSClassFromString(@"UIImageView")] || [subView isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {//找到自己需要的subView
- //支援retina高分的關鍵
- if(UIGraphicsBeginImageContextWithOptions != NULL)
- {
- UIGraphicsBeginImageContextWithOptions(subView.frame.size, NO, 0.0);
- } else {
- UIGraphicsBeginImageContext(subView.frame.size);
- }
- //獲取影象
- [subView.layer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- //儲存影象
- NSString *path = [NSHomeDirectory() stringByAppendingFormat:@"/%d.png",index];
- if ([UIImagePNGRepresentation(image) writeToFile:path atomically:YES]) {
- index += 1;
- NSLog(@"Succeeded!");
- }
- else {
- NSLog(@"Failed!");
- }
- }
- }