簡體   English   中英

iOS中的CoreGraphics:如何保存CGContext中繪制的路徑並在下一個繪制調用中檢索它們?

[英]CoreGraphics in iOS: how can I save the paths drawn in a CGContext and retrieve them in the next draw call?

編輯:

我試圖實現的是第二次調用drawRect時跳過對firstDraw()的調用。 原因是在我的真實代碼中,我要繪制很多數據點和線條,所以我認為,如果我回收先前繪制的東西,可以優化性能。 CoreGraphics完全可以做到嗎?


我希望能夠在UIView上執行多個繪圖,而不必重新繪制已經繪制的內容。

下面的代碼將以2.4秒的時間間隔執行兩次抽獎。 第一次調用后,我使用CGContextSaveGState保存狀態。 在第二個調用中,我檢索當前的圖形上下文,然后添加一些行。 但是,這似乎失敗了,因為它似乎丟失了先前的上下文。


我得到的是:

這是我在第一次抽獎中得到的:

在此處輸入圖片說明

這是第二次致電后得到的信息:

在此處輸入圖片說明

這是我第二次致電后想要得到的

在此處輸入圖片說明


這是代碼:

import UIKit

class GraphView: UIView {

    var count : Int = 0

    override func drawRect(rect: CGRect) {
        if ( count == 0){
            firstDraw()
         NSTimer.scheduledTimerWithTimeInterval(2.4, target: self, selector: "setNeedsDisplay", userInfo: nil, repeats: false)
            count++
        }
        else{
            addLinesToDraw()
        }
    }

    func firstDraw(){
        // Drawing code
        let context = UIGraphicsGetCurrentContext()

        CGContextMoveToPoint(context, 50, 50);

        CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
        CGContextSetRGBFillColor(context, 0, 0.0, 0.0, 1.0);
        CGContextAddLineToPoint(context, 60, 60);
        CGContextMoveToPoint(context, 150, 150);
        CGContextAddLineToPoint(context, 110, 90);
        CGContextSetLineWidth(context, 2);
        CGContextStrokePath(context);

        // Draw a Point as a small square
        CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 1.0);
        CGContextSetRGBFillColor(context, 0.5, 0.5, 0.5, 1.0);
        //NSLog(@"Drawing rect at point [x: %i, y: %i]", xPosition+resolution, pressureIntValue);
        CGContextFillRect(context, CGRectMake(0, 0, 10, 10));
        CGContextAddRect(context, CGRectMake(0, 0, 10, 10));
        CGContextStrokePath(context);

        CGContextSaveGState(context)
    }

    func addLinesToDraw(){
        // Drawing code
        let context = UIGraphicsGetCurrentContext()

        CGContextMoveToPoint(context, 30, 60);

        CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
        CGContextSetRGBFillColor(context, 0, 0.0, 0.0, 1.0);
        CGContextAddLineToPoint(context, 20, 20);
        CGContextMoveToPoint(context, 120, 250);
        CGContextAddLineToPoint(context, 110, 90);
        CGContextSetLineWidth(context, 2);
        CGContextStrokePath(context);

        // Draw a Point as a small square
        CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 1.0);
        CGContextSetRGBFillColor(context, 0.5, 0.5, 0.5, 1.0);
        //NSLog(@"Drawing rect at point [x: %i, y: %i]", xPosition+resolution, pressureIntValue);
        CGContextFillRect(context, CGRectMake(20, 30, 10, 10));
        CGContextAddRect(context, CGRectMake(20, 30, 10, 10));
        CGContextStrokePath(context);

        CGContextSaveGState(context)

    }
}

每次調用drawRect: ,都必須從頭開始繪制所有內容。 因此,您需要將代碼更改為以下形式:

override func drawRect(rect: CGRect) {
    firstDraw()

    if ( count == 0){
     NSTimer.scheduledTimerWithTimeInterval(2.4, target: self, selector: "setNeedsDisplay", userInfo: nil, repeats: false)
        count++
    }
    else{
        addLinesToDraw()
    }
}

如果您不喜歡此解決方案,則可以始終在單獨的CGContext中進行所有繪制,然后通過drawRect方法將其繪制到屏幕上。

使用UIGraphicsPushContext(context)而不是CGContextSaveGState(context)可以使您的上下文成為當前圖形上下文。

此處描述的差異: CGContextSaveGState與UIGraphicsPushContext

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM