2012-08-28 63 views
4

我在尋找解決方案,以便在繪圖視圖上進行美觀的縮放。 在我的應用程序中,我有一個視圖與其他UIView(用作繪圖視圖),當我在它上面畫一個筆畫時,筆畫是完美的。但是,當我縮放視圖,我有這樣的真醜效應(一像素化行程):screen shot http://data.imagup.com/10/1160827911.5509png使用繪圖視圖進行縮放

url image

有才能有正確的行程的解決方案?

我的UIViewController具有一個這樣的層次結構:

  • 的UIViewController
    • 滾動型
      • 查看可縮放(與viewForZoomingInScrollView方法所定義)
        • 圖像視圖
        • 工程視圖

非常感謝!

問候, 塞巴斯蒂安;)

回答

4

我在做一個矢量繪圖應用程序的過程中,讓我告訴你,這不是一個簡單的任務,以正確地做,需要相當多的工作。

有些問題要記住:

  • 如果你沒有使用矢量圖形(CGPaths,例如,是 向量),您將無法刪除像素化。例如,UIImage, 只有很多分辨率。
  • 爲了讓你的繪圖不會看起來像素化,你要 必須重繪一切。如果你有很多繪圖,這可能是一個昂貴的任務要執行。
  • 具有良好分辨率,同時放大幾乎是不可能的,因爲這需要過大的情況下,你的圖紙可能會超過設備

我用核芯顯卡做我的繪圖能力,所以我的方式解決這個問題是通過分配和管理多個CGContext並將它們用作緩衝區。我有一個始終保持在我的縮放級別(比例因子爲1)的上下文。這種情況隨時都會被吸引,並使得當完全放大時,沒有時間花在重繪上,因爲它已經完成了。縮放時使用另一個上下文進行繪製。當不縮放時,該上下文將被忽略(因爲無論如何都必須根據新的縮放級別重新繪製該上下文)。對於要如何進行我的變焦高水平的算法如下:

- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender 
{ 
    if(sender.state == UIGestureRecognizerStateBegan) 
    { 
     //draw an image from the unzoomedContext into my current view 

      //set the scale transformation of my current view to be equal to "currentZoom", a property of the view that keeps track of the actual zoom level 
    } 
    else if(sender.state == UIGestureRecognizerStateChanged) 
    { 
     //determine the new zoom level and transform the current view, keeping track in the currentZoom property 

      //zooming will be pixelated. 
    } 
    else if(sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled) 
    { 
      if(currentZoom == 1.0) 
      { 
        //you are done because the unzoomedContext image is already drawn into the view! 
      } 
      else 
      { 
        //you are zoomed in and will have to do special drawing 

        //perform drawing into your zoomedContext 
        //scale the zoomedContext 

        //set the scale of your current view to be equal to 1.0 

        //draw the zoomedContext into the current view. It will not be pixelated! 

        //any drawing done while zoomed needs to be "scaled" based on your current zoom and translation amounts and drawn into both contexts 
      } 

    } 

} 

這會變得更加複雜,對我來說,因爲我有一個緩衝區額外的緩衝,因爲畫的我的路徑圖像要比繪製路徑快得多時,有是很多繪圖。

在管理多個上下文,調整代碼以有效地繪製多個上下文,遵循適當的OOD,基於當前縮放和翻譯等縮放新繪圖等之間,這是一項艱鉅的任務。希望這可以激勵你,讓你走上正軌,或者你認爲擺脫這種像素化是不值得的:)

1

我有同樣的問題,並找到了解決方案:告訴視圖使用CATiledLayer作爲背景層,然後告訴視圖支持多少級別的縮放。這對我有用,當(父)視圖放大時,我的繪圖方法會自動調用。

levelsOfDetaillevelsOfDetailBias一個簡短的說明:

  • levelsOfDetail確定有多少個縮放級別總共有
  • levelsOfDetailBias確定有多少被放大在

所以我的。示例我有4個縮放級別,3個放大,1個是非縮放級別,這意味着我的視圖只在放大時重新繪製。

​​
+0

我試過這個,但是由於內存問題我的應用程序崩潰,隨機在多個地方拋出SIGABRT。我寫了一個示例來展示我如何使用這個,在實際的應用程序中,繪圖真的很大,並且有文本和圖標太多http://stackoverflow.com/questions/38191771/draw-zoomable-vector-in-ios-using -coregraphics – anoop4real

0

在您的scrollViewDidEndZooming:委託方法中使用[self setContentScaleFactor:scale];