iOS實現截屏 併合適保存

開發遊戲時,往往會有這麼一個需求:在某個成就達成或者破紀錄時,需要截個屏,然後發送到微博上與好友/粉絲分享,雖然home + 開機鍵組合可手動截屏,在Cocos2d有個CCRenderTexture類,藉助該類可很容易實現代碼截取功能。使用CCRenderTexture,我們可以截取遊戲場景、某個Layer,甚至是精靈:

  1. /**遊戲截圖  
  2.  *@param node 需要截取的控件  
  3. */  
  4. - (void)snapshotScreen:(CCNode*)node   
  5. {   
  6.     //取得屏幕大小   
  7.     CGSize winSize = [[CCDirector sharedDirector]winSize];   
  8.     CCRenderTexture* renderTexture = [CCRenderTexture renderTextureWithWidth:winSize.width   
  9.                                                                       height:winSize.height];   
  10.     [renderTexture begin];   
  11.     [node visit];   
  12.     [renderTexture end];   
  13.     [renderTexture cleanup];   
  14.     UIImage *snapshot = [renderTexture getUIImageFromBuffer];   
  15.     //把截圖保存到相冊裏   
  16.     UIImageWriteToSavedPhotosAlbum(snapshot, nil, nil, nil);   
  17. }  

  

如果遊戲支持高清模式,上面代碼截出來的圖是960 * 640大小的,這個尺寸如果要上傳到微博上,文件的大小可能會超出限制,那麼在上傳前可用先把截圖大小縮小點如480*320,以減少圖片體積:

  1. /** 調整圖片大小*/  
  2. - (UIImage *) scaleFromImage: (UIImage *) image toSize: (CGSize) size   
  3. {   
  4.     UIGraphicsBeginImageContext(size);   
  5.     [image drawInRect:CGRectMake(0, 0, size.width, size.height)];   
  6.     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();   
  7.     UIGraphicsEndImageContext();   
  8.     return newImage;   
  9. }  

  

同時可以保存

  1. 保存文件  
  2.   
  3. UIImage *m_imgFore=......;  
  4.   
  5. //png格式  
  6.   
  7. NSData *imagedata=UIImagePNGRepresentation(m_imgFore);  
  8.   
  9. //JEPG格式  
  10.   
  11. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);  
  12.   
  13. NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);  
  14.   
  15. NSString *documentsDirectory=[paths objectAtIndex:0];   
  16.   
  17. NSString *savedImagePath=[documentsDirectorystringByAppendingPathComponent:@"saveFore.png"];  
  18.   
  19. [imagedata writeToFile:savedImagePath atomically:YES];  
  20.   
  21. 或者  
  22.   
  23. [fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.png"] contents:data attributes:nil];    將圖片保存爲PNG格式  
  24.   
  25.  [fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.jpg"] contents:data attributes:nil];   將圖片保存爲JPEG格式  


如果不是cocos2D開發,則使用如下代碼:

方法1:

  1. -(void)screenShots  
  2. {  
  3.     CGSize imageSize = [[UIScreen mainScreen] bounds].size;  
  4.     if (NULL != UIGraphicsBeginImageContextWithOptions) {  
  5.         UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);  
  6.     }  
  7.     else  
  8.     {  
  9.         UIGraphicsBeginImageContext(imageSize);  
  10.     }  
  11.       
  12.     CGContextRef context = UIGraphicsGetCurrentContext();  
  13.       
  14.     for (UIWindow * window in [[UIApplication sharedApplication] windows]) {  
  15.         if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {  
  16.             CGContextSaveGState(context);  
  17.             CGContextTranslateCTM(context, [window center].x, [window center].y);  
  18.             CGContextConcatCTM(context, [window transform]);  
  19.             CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y);  
  20.             [[window layer] renderInContext:context];  
  21.               
  22.             CGContextRestoreGState(context);  
  23.         }  
  24.     }  
  25.       
  26.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  27.       
  28.     UIGraphicsEndImageContext();  
  29.     UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);  
  30.     NSLog(@"Suceeded!");  
  31. }  

(注意:方法1截圖後會是豎屏,所以需要配合UIImage旋轉的方法判斷方向後旋轉才能合適保存,見文章旋轉翻轉UIImage 不是UIImageView 適用於源圖像的處理,例如截圖後旋轉)


所以再提供另外一方法:

方法2

  1. UIView *view = [[[[[UIApplication sharedApplication] windows] objectAtIndex:1] subviews] lastObject];//獲得某個window的某個subView  
  2.    
  3.     NSInteger index = 0;//用來給保存的png命名  
  4.     for (UIView *subView in [view subviews]) {//遍歷這個view的subViews  
  5.         if ([subView isKindOfClass:NSClassFromString(@"UIImageView")] || [subView isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {//找到自己需要的subView  
  6.             //支持retina高分的關鍵  
  7.             if(UIGraphicsBeginImageContextWithOptions != NULL)  
  8.             {  
  9.                 UIGraphicsBeginImageContextWithOptions(subView.frame.size, NO, 0.0);  
  10.             } else {  
  11.                 UIGraphicsBeginImageContext(subView.frame.size);  
  12.             }              
  13.    
  14.             //獲取圖像  
  15.             [subView.layer renderInContext:UIGraphicsGetCurrentContext()];  
  16.             UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  17.             UIGraphicsEndImageContext();  
  18.    
  19.             //保存圖像  
  20.             NSString *path = [NSHomeDirectory() stringByAppendingFormat:@"/%d.png",index];  
  21.             if ([UIImagePNGRepresentation(image) writeToFile:path atomically:YES]) {  
  22.                 index += 1;  
  23.                 NSLog(@"Succeeded!");  
  24.             }  
  25.             else {  
  26.                 NSLog(@"Failed!");  
  27.             }  
  28.         }  
  29.     }  
發佈了4 篇原創文章 · 獲贊 8 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章