Juan Vidal
Juan Vidal

Reputation: 51

Problems drawing with CGContext in iOS7

I developed a draw tool for iPad.

When using the tool on iOS6, things work fine, but when the tool is used on iOS7 the draw lines do not appear after the touch end.

I am using CGContextAddLineToPoint and UIGraphicsGetCurrentContext for drawing.

Thanks

this is the code

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    pointerUndone++;
//    NSLog(@"undone store %f",pointerUndone);

    UITouch *touch = [[event allTouches] anyObject];

    if (DEBUG_MESSAGES == YES) NSLog(@"Draw");

    if([touch tapCount] == 2){
        //drawImage.image = nil;//double touch for clear all current stroke
    }else if([touch tapCount] == 3){
        [self finishParentView];
    }

    self.currentStroke = [[NSMutableArray alloc] init];

    location = [touch locationInView:touch.view];
    lastClick = [NSDate date];

    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 0;

    [self.currentStroke addObject:[NSValue valueWithCGPoint:lastPoint]];

    [super touchesBegan:touches withEvent:event];
}

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

    UITouch *touch = [touches anyObject];
    currentPoint = [touch locationInView:self.view];



    UIGraphicsBeginImageContext(CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width));
    [drawImage.image drawInRect:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapButt);

    if(self.draftState == NO){
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), self.sizeStroke);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), R/255, G/255, B/255, alpha);    //draw
    }else{

        if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
        else
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);    //delete
    }

    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

    CGContextStrokePath(UIGraphicsGetCurrentContext());
    [drawImage setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)];

    //drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

    drawImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext());

    UIGraphicsEndImageContext();
    lastPoint = currentPoint;

    [self.view addSubview:drawImage];

    [self.currentStroke addObject:[NSValue valueWithCGPoint:currentPoint]];

}

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

    if([self.currentStroke  count]>1){
        DatabaseManager *db = [[DatabaseManager alloc] init];
        [db deleteRedone];
        NSMutableDictionary *strokeWithColor = [[NSMutableDictionary alloc] init];
        NSDictionary *colorDictionary = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:[NSNumber numberWithDouble:R],[NSNumber numberWithDouble:G],[NSNumber numberWithDouble:B], nil] forKeys:[NSArray arrayWithObjects:@"R",@"G",@"B", nil]];
        [strokeWithColor setObject:[NSString stringWithFormat:@"%.2f",self.sizeStroke] forKey:@"size"];
        if(self.draftState == NO){
            [strokeWithColor setObject:@"pencil" forKey:@"type"];
            [strokeWithColor setObject:[self getSelectedColor] forKey:@"colorString"];
        }else{
            [strokeWithColor setObject:@"draft" forKey:@"type"];
            [strokeWithColor setObject:@"TRANSPARENT" forKey:@"colorString"];
        }

        [strokeWithColor setObject:colorDictionary forKey:@"color"];
        //[strokeWithColor setObject:[self getSelectedColor] forKey:@"colorString"];
        [strokeWithColor setObject:self.currentStroke forKey:@"stroke"];
        [strokeWithColor setObject:@"YES" forKey:@"visible"];

        [self.strokes addObject:strokeWithColor];
        [self registerStrokeInDataBase:self.currentStroke];
    }
}

Upvotes: 1

Views: 1316

Answers (1)

itechnician
itechnician

Reputation: 1655

To find a instant solution, Replace this line

 drawImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext());

with

[drawImage performSelectorInBackground:@selector(setImage:) withObject:UIGraphicsGetImageFromCurrentImageContext()];

If you need an elaborate and accurate solution try to replace the MoveTo with CGMutablepath . Hope this helps.

Upvotes: 3

Related Questions