在UIImage上绘图的问题

时间:2012-06-19 10:02:52

标签: ios cgcontext

我使用以下代码绘制图像。所有的触摸代表都在工作,但绘图不会在UIImage上呈现。这是代码。我无法确定问题所在。提前谢谢。

- (id)initWithCoder:(NSCoder *)aDecoder {

    if ((self = [super initWithCoder:aDecoder])) {
        [self setupVariables];
    }

    return self;
}

- (void)drawPic:(UIImage *)thisPic {    
    myPic = thisPic;

    [myPic retain];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {

    float newHeight;
    float newWidth;
    float xPos;
    float yPos;
    float ratio;

    if (myPic != NULL) {
        ratio = myPic.size.height/460;

        if (myPic.size.width/320 > ratio) {
            ratio = myPic.size.width/320;
        }

        newHeight = myPic.size.height/ratio;
        newWidth = myPic.size.width/ratio;

        xPos = (320 - newWidth) / 2;
        yPos = (460 - newHeight) / 2;

        [myPic drawInRect:CGRectMake(xPos, yPos, newWidth, newHeight)];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    NSArray *allTouches = [[event allTouches] allObjects];

    if ([allTouches count] > 1) {
        return;
    }
    else {
        [self drawPoint:[allTouches objectAtIndex:0]];
    }    
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    NSArray *allTouches = [[event allTouches] allObjects];

    if ([allTouches count] > 1) {
        return;
    }
    else {
        [self drawPoint:[allTouches objectAtIndex:0]];

        self.previousPoint = nil;
        self.point = nil;
    }    
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

    NSArray *allTouches = [[event allTouches] allObjects];

    if ([allTouches count] > 1) {
        return;
    }
    else {
        [self drawPoint:[allTouches objectAtIndex:0]];

        self.previousPoint = nil;
        self.point = nil;
    }        
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    NSArray *allTouches = [[event allTouches] allObjects];

    if ([allTouches count] > 1) {
        return;
    }
    else {
        [self drawPoint:[allTouches objectAtIndex:0]];
    }    
}


- (void)dealloc {

    CGContextRelease(offScreenBuffer);
    [point release];
    [previousPoint release];
    [super dealloc];
}

- (void)setupVariables {

    self.point = nil;
    self.previousPoint = nil;
    offScreenBuffer = [self setupBuffer];
}

- (CGContextRef)setupBuffer {

    CGSize size = self.bounds.size;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context =  CGBitmapContextCreate(NULL,size.width,size.height,8,size.width*4, colorSpace,kCGImageAlphaPremultipliedLast);

    CGColorSpaceRelease(colorSpace);

    CGContextTranslateCTM(context, 0, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    return context;    
}

- (void)drawToBuffer {

    //                  Red  Gr   Blu  Alpha
    CGFloat color[4] = {0.0, 1.0, 0.0, 1.0};

    if (self.previousPoint != nil) {
        CGContextSetRGBStrokeColor(offScreenBuffer, color[0], color[1], color[2], color[3]);

        CGContextBeginPath(offScreenBuffer);
        CGContextSetLineWidth(offScreenBuffer, 10.0);
        CGContextSetLineCap(offScreenBuffer, kCGLineCapRound);

        CGContextMoveToPoint(offScreenBuffer, previousPoint.location.x, previousPoint.location.y);
        CGContextAddLineToPoint(offScreenBuffer, point.location.x, point.location.y);

        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeColor);

        CGContextDrawPath(offScreenBuffer, kCGPathStroke);
    }    
}

- (void)drawPoint:(UITouch *)touch {

    PointLocation *currentLoc = [[PointLocation alloc] init];

    currentLoc.location = [touch locationInView:self];

    self.previousPoint = self.point;

    self.point = currentLoc;
    [self drawToBuffer];

    [self setNeedsDisplay];

    [currentLoc release];
}

2 个答案:

答案 0 :(得分:0)

这很简单。您需要在drawRect:中完成所有绘图,而您只是在drawRect:中绘制图像,这就是您只能看到图像的原因。

答案 1 :(得分:0)

-(void)drawPic:(UIImage *)thisPic {
//code for UIViewContentModeScaleAspectFit if needed
self.backgroundColor = [UIColor colorWithPatternImage:thisPic];
[self setNeedsDisplay];
}

drawRect 中使用此方法:

  

[myPic drawInRect:CGRectMake(xPos,yPos,newWidth,newHeight)];

修改

- (void)drawRect:(CGRect)rect {

if (!myDrawing) {

//      CGFloat scale = 0.5;
//      CATransform3D transform = CATransform3DMakeScale(scale, scale, scale);
//      [self layer].transform = transform;
    myDrawing = [[NSMutableArray alloc] initWithCapacity:0];
    self.lineOnOffArray = [[NSMutableArray alloc] initWithCapacity:0];

    //arrPenThickness = [[NSMutableArray alloc] initWithCapacity:0];
}

CGContextRef ctx = UIGraphicsGetCurrentContext();

if ([myDrawing count] > 0) {

    for (int i = 0 ; i < [myDrawing count] ; i++) {
        NSArray *thisArray = [myDrawing objectAtIndex:i];

        NSString  *onOffFlag = [self.lineOnOffArray objectAtIndex:i];

        if (([thisArray count] > 2) && (onOffFlag == @"1")){
            float penT = [[arrPenThickness objectAtIndex:i] floatValue];
            NSString* penC = [arrPenColors objectAtIndex:i];

            NSArray *arr = [penC componentsSeparatedByString:@","];

            float   r = [[arr objectAtIndex:0]floatValue];
            float   g = [[arr objectAtIndex:1]floatValue];
            float   b = [[arr objectAtIndex:2]floatValue];
            float   a = [[arr objectAtIndex:3]floatValue];
            CGContextSetRGBStrokeColor(ctx, 
                                       r/255.0,
                                       g/255.0,
                                       b/255.0,
                                       a);

            CGContextSetLineCap(ctx, kCGLineCapRound);
            CGContextSetLineWidth(ctx, penT);
            float thisX = [[thisArray objectAtIndex:0] floatValue];
            float thisY = [[thisArray objectAtIndex:1] floatValue];

            CGContextBeginPath(ctx);


            for (int j = 2; j < [thisArray count] ; j+=2) {

                CGContextMoveToPoint(ctx, thisX, thisY);
                thisX = [[thisArray objectAtIndex:j] floatValue];
                thisY = [[thisArray objectAtIndex:j+1] floatValue];
                CGContextAddLineToPoint(ctx, thisX,thisY);
            }

            CGContextStrokePath(ctx);

        }
    }
}

}