0

很难说,所以我上传了一张图片来显示我的问题:http: //i42.tinypic.com/2eezamo.jpg

基本上在drawRect中,我会在手指触摸时从touchesMoved画线,我会调用“needsDisplayInRect”进行重绘。但是我发现第一行已经完成,第二行会清除rect部分,所以之前的一些绘图就没有了。

这是我的实现:

enter code here

-(void) drawRect:(CGRect)rect{
//[super drawRect: rect];
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawSquiggle:squiggle at:rect inContext:context];
}

- (void)drawSquiggle:(Squiggle *)squiggle at:(CGRect) rect inContext:(CGContextRef)context
{   

CGContextSetBlendMode(context, kCGBlendModeMultiply);
UIColor *squiggleColor = squiggle.strokeColor; // get squiggle's color
CGColorRef colorRef = [squiggleColor CGColor]; // get the CGColor
CGContextSetStrokeColorWithColor(context, colorRef);        

NSMutableArray *points = [squiggle points]; // get points from squiggle
// retrieve the NSValue object and store the value in firstPoint
CGPoint firstPoint; // declare a CGPoint
[[points objectAtIndex:0] getValue:&firstPoint];
// move to the point
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
// draw a line from each point to the next in order

for (int i = 1; i < [points count]; i++)
{
    NSValue *value = [points objectAtIndex:i]; // get the next value
    CGPoint point; // declare a new point
    [value getValue:&point]; // store the value in point

    // draw a line to the new point
    CGContextAddLineToPoint(context, point.x, point.y);
} // end for
CGContextStrokePath(context); 
}

我尝试实现这一点,但似乎它没有按我的预期工作。每次在视图上调用 setNeedsDisplay 时,我都实现了一个视图来绘制一个正方形。我将 CGImageRef 图像和 CGBitmapContextRef 上下文放入 ivar 并在视图初始化时分配它们。

#import "View.h"


@implementation View


#define bitsPerComponent 8
#define bitsPerPixel 4*bitsPerComponent
#define bytesPerPixel 4


- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
    lastPoint = CGPointMake(50, 50);
    size_t width = frame.size.width;
    size_t height = frame.size.height;

    CFMutableDataRef data = CFDataCreateMutable( NULL , width * height * bytesPerPixel );  
    CGDataProviderRef provider = CGDataProviderCreateWithCFData( data );
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

    /*CGImageRef - ivar */ image = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, width*bytesPerPixel, colorspace, kCGImageAlphaPremultipliedLast, provider, NULL, NO, kCGRenderingIntentDefault);
    /*CGBitmapContextRef - ivar */ context = CGBitmapContextCreate(CFDataGetMutableBytePtr(data), width, height, bitsPerComponent, width*bytesPerPixel , colorspace, kCGImageAlphaPremultipliedLast);

    CFRelease( data ); // retained by provider I think
    CGDataProviderRelease( provider ); // retained by image
    CGColorSpaceRelease(colorspace);
}
return self;
}

- (void)drawRect:(CGRect)rect {
NSLog(@"test");
CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]);
CGContextFillRect(context, CGRectMake(lastPoint.x, lastPoint.y, 100, 100));
lastPoint = CGPointMake( ((int)lastPoint.x+50) % 380 , ((int)lastPoint.y+50) % 220 );
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, 100, 100),image);
}

- (void)dealloc {
[super dealloc];
}


@end
4

1 回答 1

5

UIGraphicsGetCurrentContextin返回的图形上下文归drawRect:系统所有,并在绘制过程开始时清除。创建您自己的CGContext以记录您的绘图并根据需要将其渲染到drawRect:上下文中。

创建一个成对的CGBitmapContextCGImage那个指代相同CGDataProvider并且具有相同大小、色彩空间和其他属性的对象。在您认为合适的时间和方式绘制到该上下文中,然后drawRect:用于CGContextDrawImage显示它。

或者如果你只是在做一系列的行,你可以构建一个CGMutablePath并将其渲染到系统上下文中。

编辑:

查看它们各自的头文件CGImageCreateCGBitmapContextCreate大多数论点是相同的。位图上下文需要一个原始数据指针,而图像需要一个 CGDataProvider。这是代码外观的粗略草图。最后,您可以绘制到上下文中,然后使用图像渲染到另一个上下文中。

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

有很多空白要填写,但这应该可以帮助您入门。您可以使用 malloc 而不是 CFData 作为缓冲区。

于 2010-04-29T05:19:28.813 回答