2013-10-18 42 views
-2

我最近使我的應用程序與最新的視網膜iPad兼容,但我的繪圖變得模糊。然後我嘗試改變contextWithOptions,但當我繪製時,它變得非常遲緩和參差不齊。我試過的一切都不起作用。有任何想法嗎?在Retina顯示屏上繪圖?

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    mouseSwiped = YES; 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
    if (UIGraphicsBeginImageContextWithOptions != NULL) { 
     UIGraphicsBeginImageContextWithOptions((self.view.frame.size), NO, 1); 
     //becomes laggy if I set it to 0 or 2. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
    } else { 
     UIGraphicsBeginImageContext(self.view.frame.size); 
    } 
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); 

    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    [self.tempDrawImage setAlpha:opacity]; 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    if(!mouseSwiped) { 
     UIGraphicsBeginImageContext(self.view.frame.size); 
     [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     CGContextFlush(UIGraphicsGetCurrentContext()); 
     self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 

     UIGraphicsBeginImageContextWithOptions((self.view.frame.size), NO, 0); 
    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0]; 
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity]; 
    self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    self.tempDrawImage.image = nil; 
    UIGraphicsEndImageContext(); 
} 

也許當用戶結束觸摸我應該改變形象?請幫忙嗎?

回答

1

正確,您不想嘗試爲視圖中的每個觸摸創建圖像。你只是想繪製你的道路。或者,更準確地說,你需要你的觸摸手勢來(a)更新你的模型,它由一堆路徑組成(每個路徑本身就是一組點);和(b)調用必要的調用來更新用於更新模型的UI。

要渲染圖中,你有兩種基本方法:

  1. 創建UIView子類具有drawRect方法,通過你的系列路徑進行迭代,並且吸引過來;或

  2. 創建一個CAShapeLayer它使用您的路徑,然後做[self.view.layer addSublayer:layer]

兩種技術都沒問題。但請注意,在這些手勢中,您都不會使用任何UIGraphicsBeginImageContext,UIGraphicsGetImageFromCurrentImageContextUIGraphicsEndImageContext。只有當你想將繪圖保存爲圖像時(大概在用戶的繪圖手勢完成後),你纔會這樣做。

但是,如果你這樣做,即使在視網膜屏幕上,用戶的手勢也不會有任何問題。操作圖像是一個緩慢且內存效率低下的過程,因此您只需要謹慎操作。順便說一下,如果你將這一系列路徑作爲繪圖模型的中心部分,它也會帶來其他機會。例如,只需從模型中刪除該路徑對象並重新渲染視圖,就可以輕鬆「撤消」路徑。但是如果你將它保存爲圖像,刪除幾個stokes就變得不切實際。最後,你必須決定你的應用程序是否將是一個矢量繪圖應用程序(其中保存的文件是路徑數組,也可能有保存圖像的選項),或者它是更多的位圖編輯器(最後放棄矢量信息並只保存位圖)。無論採用哪種方法,在手勢過程中使用路徑對象都可能會使UI更加靈敏。

相關問題