試試這個
-(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];
}
-(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;
}
希望對大家有所幫助
我知道如何使用Quartz漆,問題是如何通過他的手指,讓用戶的油漆。像一個小的Visio類型的應用:) – Eugen 2012-03-20 07:36:40
也許這個鏈接將幫助你: http://www.techotopia.com/index.php/An_iOS_4_iPhone_Graphics_Drawing_Tutorial_using_Quartz_2D – 2012-03-20 07:49:11
亞歷克斯,請仔細閱讀我的問題,並檢查您的評論,在哪裏做您可以看到與使用觸摸事件的用戶的交互。正如我所說的,我非常清楚如何使用Quartz進行繪製,問題在於通常的技巧是讓用戶繪製某種形狀,就像一些圖形編輯器一樣。 – Eugen 2012-03-20 22:18:11