2013-02-09 60 views
4

我有一個繪圖應用程序的種類,我想創建一個Canvas UIView的快照(在屏幕上和屏幕上),然後將其縮小。我所做的代碼在iPad 3上永遠不會流血。模擬器沒有任何延遲。畫布是2048x2048。捕獲UIView的屏幕截圖 - 慢速性能

有沒有另一種方法我應該這樣做?或者我在代碼中錯過了一些東西?

謝謝!

-(UIImage *) createScreenShotThumbnailWithWidth:(CGFloat)width{ 
     // Size of our View 
    CGSize size = editorContentView.bounds.size; 


     //First Grab our Screen Shot at Full Resolution 
    UIGraphicsBeginImageContext(size); 
    [editorContentView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

     //Calculate the scal ratio of the image with the width supplied. 
    CGFloat ratio = 0; 
    if (size.width > size.height) { 
     ratio = width/size.width; 
    } else { 
     ratio = width/size.height; 
    } 

     //Setup our rect to draw the Screen shot into 
    CGSize newSize = CGSizeMake(ratio * size.width, ratio * size.height); 

     //Send back our screen shot 
    return [self imageWithImage:screenShot scaledToSize:newSize]; 

} 
+0

重要的是要記住,模擬器只是這一點很重要,一個模擬器,而不是一個iPad,iPhone或iPod的性能真實再現。任何代碼都可以訪問計算機的全部資源,這些資源將比iPad 3強大得多。正如AliSoftware在下面提到的,在繪製圖像之前縮放圖像可能是一個更好的主意,這樣您可以切出你目前正在做的工作中大約有50%。 – 2013-02-09 12:38:50

回答

2

是否使用了「時間探查」儀器(「產品」菜單 - >「個人資料」),以檢查你的代碼,你花大部分的時間? (當然使用它與您的設備,而不是模擬器,以實現現實的分析)。我猜想它不是在你的問題中引用的圖像捕捉部分,而是在你的縮放方法imageWithImage:scaledToSize:方法中。

相反的情況下,在其全尺寸渲染圖像,然後將圖像重新縮放到最終大小,你應該在上下文直接在預期大小通過應用一些仿射變換,上下文渲染層。

所以,簡單地使用上UIGraphicsGetCurrentContext()CGContextConcatCTM(someScalingAffineTransform);你行UIGraphicsBeginImageContext(size);之後,應用的縮放仿射變換,它將使層以不同的比例/尺寸呈現。

這樣,它會直接呈現爲預期的大小,這將是不是100%被渲染,然後有你事後重新調整它在耗費時間的方式快得多,

+0

謝謝您的建議。這對我目前的解決方案是一個很好的引導。使用'CGContextConcatCTM'我可以在一段時間內減少一秒。儘管我最終使用了裁剪2秒的'UIGraphicsBeginImageContextWithOptions'。 – scooter133 2013-02-12 17:48:44

+0

'UIGraphicsBeginImageContextWithOptions'好像是在做我想做的事情,儘管圖像的物理尺寸從未改變過。我最終使用'CGContextConcatCTM'謝謝 – scooter133 2013-02-12 18:56:08

+0

'UIGraphicsBeginImageContextWithOptions'中的'scale'參數會影響像素點比例(即非視網膜爲1,視網膜爲2),而不是渲染大小的轉換矩陣,所以圖像的物理尺寸當然不受此影響。這就是爲什麼我建議'CGContextConcatCTM(ctx,CGAffineTransformMakeScale(ratio,ratio))'而不是這個。 – AliSoftware 2013-02-12 20:37:27

0

謝謝AliSoftware,這裏是我結束了使用的代碼:

-(UIImage *) createScreenShotThumbnailWithWidth:(CGFloat)width{ 
     if (IoUIDebug & IoUIDebugSelectorNames) { 
      NSLog(@"%@ - %@", INTERFACENAME, NSStringFromSelector(_cmd)); 
     } 
      // Size of our View 
     CGSize size = editorContentView.bounds.size; 

      //Calculate the scal ratio of the image with the width supplied. 
     CGFloat ratio = 0; 
     if (size.width > size.height) { 
      ratio = width/size.width; 
     } else { 
      ratio = width/size.height; 
     } 
     CGSize newSize = CGSizeMake(ratio * size.width, ratio * size.height); 

      //Create GraphicsContext with our new size 
     UIGraphicsBeginImageContext(newSize); 

      //Create Transform to scale down the Context 
     CGAffineTransform transform = CGAffineTransformIdentity; 
     transform = CGAffineTransformScale(transform, ratio, ratio); 

      //Apply the Transform to the Context 
     CGContextConcatCTM(UIGraphicsGetCurrentContext(),transform); 

      //Render our Image into the the Scaled Graphic Context 
     [editorContentView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

      //Save a copy of the Image of the Graphic Context 
     UIImage* screenShot = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     return screenShot; 

    } 
+0

'renderInContext:'現在是食客。樂器說renderSubLayers約爲800ms,前景層爲800ms。 – scooter133 2013-02-12 19:56:38