2016-02-12 2 views
0

Ceci est mon code et il fait un dessin très étrange lorsqu'il est exécuté. De plus, l'image commence à disparaître lentement en descendant l'imageview. S'il vous plaît aidez-moi à ceCGcontext dessin sur l'image ne fonctionne pas

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [[event allTouches] anyObject]; 

// if ([touch tapCount] == 2) 
// { 
//  imageView.image = nil; 
// } 

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

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

[super touchesBegan:touches withEvent:event]; 
} 

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 
{mouseSwiped = YES; 

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

UIGraphicsBeginImageContext(imageView.image.size); 

[imageView.image drawInRect:CGRectMake(0, 44, imageView.image.size.width, imageView.image.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 

imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
// lastPoint = currentPoint; 


} 

De plus, les lignes de son dessin sont de forme bizarre et ils sont en train de disparaître continously

Répondre

0

Votre image se déplace, parce que vous Hardcoded contrebalance en 44 points sur chaque redessiner.

Un dessin bizarre est probablement le résultat d'une utilisation incorrecte du système de coordonnées. Vous recevez l'emplacement tactile dans les coordonnées de la vue, mais dessinez dans les coordonnées de l'image. Le moyen le plus simple de résoudre ce problème consiste à créer un contexte avec une taille égale à la taille de la vue au lieu de la taille de l'image. Il suffit d'utiliser imageView.bounds.size au lieu de imageView.image.size. Notez que je suppose que vous utilisez le mode "Scale to Fill" dans votre vue d'image.

code de dessin entier après modifications:

UIGraphicsBeginImageContext(self.imageView.bounds.size); 

[self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.bounds.size.width, self.imageView.bounds.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 

self.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

En outre, votre solution n'est pas optimale en termes de performance. Je suggère de dessiner le chemin séparément, au lieu de mettre à jour l'image imageView sur chaque mouvement de contact.