2013-10-18 48 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更加灵敏。

相关问题