2010-05-04 44 views
1

我正在創建一個簡單的繪圖應用程序,並希望根據用戶的觸摸事件繪製矩形。我可以使用Core Graphics從touchesBegan到touchesEnded中的點繪製矩形。這個矩形會在觸發touchesEnded事件後顯示。使用Core Graphics和實時預覽繪製矩形

但是,我希望能夠以'現場'的方式做到這一點。意思是,當用戶拖動他們的手指時,矩形被更新並顯示。這可能嗎?任何幫助將不勝感激,謝謝!

UPDATE:

我能得到這個使用下面的代碼工作。它適用於小圖像,但它對於大圖像非常緩慢。我意識到我的解決方案效率非常低,想知道是否有人可以提出更好的解決方案。謝謝。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     UITouch *touch = [touches anyObject]; 
     _firstPoint = [touch locationInView:self.view]; 
     _firstPoint.y -= 40; 
     _lastPoint = _firstPoint; 
    } 

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     UITouch *touch = [touches anyObject]; 
     CGPoint currentPoint = [touch locationInView:self.view]; 
     currentPoint.y -= 40; 

     [self drawSquareFrom:_firstPoint to:currentPoint]; 

     _lastPoint = currentPoint; 
    } 

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     UITouch *touch = [touches anyObject]; 
     CGPoint currentPoint = [touch locationInView:self.view]; 
     currentPoint.y -= 40; 

     [self drawSquareFrom:_firstPoint to:currentPoint]; 

     [_modifiedImage release]; 
     _modifiedImage = [[UIImage alloc] initWithCGImage:_imageView.image.CGImage]; 
    } 

    - (void)drawSquareFrom:(CGPoint)firstPoint to:(CGPoint)lastPoint 
    { 
     _imageView.image = _modifiedImage; 

     CGFloat width = lastPoint.x - firstPoint.x; 
     CGFloat height = lastPoint.y - firstPoint.y; 

     _path = [UIBezierPath bezierPathWithRect:CGRectMake(firstPoint.x, firstPoint.y, width, height)]; 

     UIGraphicsBeginImageContext(_originalImage.size); 
     [_imageView.image drawInRect:CGRectMake(0, 0, _originalImage.size.width, _originalImage.size.height)]; 

     _path.lineWidth = 10; 
     [[UIColor redColor] setStroke]; 
     [[UIColor clearColor] setFill]; 
     [_path fill]; 
     [_path stroke]; 

     _imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 
+0

爲什麼不創建CALayer對象組合的圖像,而不是繪製到位圖圖像中? (例如,Illustrator與Photoshop)在這種情況下,您可以輕鬆地將CAShapeLayer與矩形路徑添加到您的文檔中。當用戶完成拖動時將其提交到文檔。 – nielsbot 2013-01-06 01:48:42

+0

在任何情況下,您都可以在文檔視圖中使用帶有矩形路徑的CAShapeLayer預覽當前圖像。 – nielsbot 2013-01-06 01:49:09

回答

0

你現在正在做什麼正確的是創造一個配對CGImageCGBitmapContext每次調用drawSquareFrom:to:每次處置他們。相反,請創建一個配對的CGImageCGBitmapContext,您可以爲每個呼叫重複使用該配對。

-(void) drawSquareFrom:(CGPoint)from to:(CGPoint)to { 
    CGContextRef context = [self offscreenContext]; 
    CGRect draw , full = [self offscreenContextBounds]; 

    draw.origin = from; 
    draw.size.width = to.x - from.x; 
    draw.size.height = to.y - from.y; 
    draw = CGRectStandardize(draw); 

    [[UIColor redColor] setStroke]; 
    [[UIColor clearColor] setFill]; 
    CGContextClearRect(context , full); 
    CGContextFillRect(context , draw); 
    CGContextStrokeRectWithWidth(context , draw , 10); 
    [_imageView setNeedsDisplay]; 
} 

您創建一個配對CGImage和CGContext上這樣的,雖然有一些...來填補,所有的MYX變量意在類成員。

-(CGContextRef) offscreenContext { 
    if (nil == myContext) { 
     size_t width = 400; 
     size_t height = 300; 
     CFMutableDataRef data = CFDataCreateMutable(NULL , width * height * 4); // 4 is bytes per pixel 
     CGDataProviderRef provider = CGDataProviderCreateWithCFData(data); 
     CGImageRef image = CGImageCreate(width , height , ... , provider , ...); 
     CGBitmapContextRef context = CGBitmapContextCreate(CFDataGetMutableBytePtr(data) , width , height , ...); 
     CFRelease(data); // retained by provider I think 
     CGDataProviderRelease(provider); // retained by image 

     myImage = image; 
     myContext = context; 
     myContextFrame.origin = CGPointZero; 
     myContextFrame.size.width = width; 
     myContextFrame.size.height = height; 
     _imageView.image = [UIImage imageWithCGImage:image]; 
    } 

    return myContext; 
} 
-(CGRect) offscreenContextBounds { 
    return myContextFrame; 
} 

這將_imageView.image設置爲新創建的圖像。在drawSquareFrom:to:我假設setNeedsDisplay足以繪製所做的更改,但它可能是您需要分配一個新的UIImage每次包裝相同的CGImage。

0

只需使用兩個上下文。 在一個你只是「預覽」繪製矩形,你可以清除它在TouchesMoved。 On TouchesEnd你終於畫出你的原始上下文,然後在圖像上。

相關問題