Annuler le dessin dans Paint Application

Je fais une application de peinture en utilisant le framework OpenGl et je suis bloqué à l'option UNDO / REDO. Le code que j'ai implémenté est comme ceci:

-(void)undo_called { artbrushAppDelegate *app=(artbrushAppDelegate *)[[UIApplication sharedApplication]delegate]; mbrushscale=app.brushscale; brushimage=app.brush_image; Erase=YES; [self playRayundo]; } -(void)playRayundo { artbrushAppDelegate *app=(artbrushAppDelegate *)[[UIApplication sharedApplication]delegate]; glColor4f(app.r1g, app.b1g, app.g1g, 0); NSLog(@"%f",app.r1g); if(undo != NULL) { for(int l = 0; l < [undo count]; l++) { //replays my writRay -1 because of location point for(int p = 0; p < [[undo objectAtIndex:l]count]-1; p ++) { [self drawErase:[[[undo objectAtIndex:l]objectAtIndex:p]CGPointValue] toPoint:[[[undo objectAtIndex:l]objectAtIndex:p + 1]CGPointValue]]; } } } Erase=NO; glColor4f(app.rg, app.bg, app.gg, kBrushOpacity); } -(void) drawErase:(CGPoint)start toPoint:(CGPoint)end { static GLfloat* eraseBuffer = NULL; static NSUInteger eraseMax = 64; NSUInteger vertexCount = 0, count, i; [EAGLContext setCurrentContext:context]; glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); // Convert locations from Points to Pixels CGFloat scale = 1.0;//self.contentScaleFactor; start.x *= scale; start.y *= scale; end.x *= scale; end.y *= scale; // Allocate vertex array buffer if(eraseBuffer == NULL) eraseBuffer = malloc(eraseMax * 2 * sizeof(GLfloat)); // Add points to the buffer so there are drawing points every X pixels count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y)) / kBrushPixelStep), 1); for(i = 0; i < count; ++i) { if(vertexCount == eraseMax) { eraseMax = 2 * eraseMax; eraseBuffer = realloc(eraseBuffer, eraseMax * 2 * sizeof(GLfloat)); } eraseBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count); eraseBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count); vertexCount += 1; } [self ChangebrushPic:brushimage]; //the erase brush color is transparent. glEnable(GL_POINT_SPRITE_OES); glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE); glPointSize(64/mbrushscale); // Render the vertex array glVertexPointer(2, GL_FLOAT, 0, eraseBuffer); glDrawArrays(GL_POINTS, 0, vertexCount); // Display the buffer glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); [context presentRenderbuffer:GL_RENDERBUFFER_OES]; // at last restore the mixed-mode glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } 

Ce code n'est pas efficace et il y a tellement de fautes. Jetez un oeil aux images avant et après Annuler.

Avant: http://imageshack.com/f/g1screenshot20110714at121p/

Après: https://imageshack.com//

Donc, je veux save l'image dans le tampon chaque fois que le toucher de l'user se termine et appeler l'image précédente sur UNDO..Peut-on m'aider à comment stocker l'image dans le tampon et récupérer sur UNDO ?? J'ai essayé de find son code d'exemple mais je n'ai pas pu find ..

Merci..

D'accord. Je post une réponse à ma propre question car tant de gens semblent se référer à cette question.

Il y a une application nommée TouchPainter et son code source est disponible. Il contient dessin, mélange de colors, Undo / Redo (Amazing .. !!), Save / Open dessins ..

Note: Il est possible que vous ayez besoin d'une connaissance très approfondie de l' objective C et du motif de design (je ne sais toujours pas quel est le motif de design, mais je l'ai trouvé, donc je partage …). Le code source de l'application entière est expliqué dans ce livre "Apress.Pro Objective-C Design Patterns pour iOS" .

J'espère que cela vous sera utile .. 🙂 Bonne chance ..

  Hope this may be help u.. This code for UIBezierPath Drawing. -(void)undoButtonClicked { if([pathArray count]>0){ UIBezierPath *_path = [pathArray lastObject]; [bufferArray addObject:_path]; [bufferColorArray addObject:[PathColorArray lastObject]]; [pathArray removeLastObject]; [PathColorArray removeLastObject]; [bufferDrawType addObject:[pathDrawType lastObject]]; [pathDrawType removeLastObject]; [bufferDrawOpacity addObject:[pathDrawOpacity lastObject]]; [pathDrawOpacity removeLastObject]; [bufferDrawLineWidth addObject:[pathDrawLineWidth lastObject]]; [pathDrawLineWidth removeLastObject]; [self setNeedsDisplay]; } } -(void)redoButtonClicked { if([bufferArray count]>0){ UIBezierPath *_path = [bufferArray lastObject]; [pathArray addObject:_path]; [bufferArray removeLastObject]; [PathColorArray addObject:[bufferColorArray lastObject]]; [bufferColorArray removeLastObject]; [pathDrawType addObject:[bufferDrawType lastObject]]; [bufferDrawType removeLastObject]; [pathDrawOpacity addObject:[bufferDrawOpacity lastObject]]; [bufferDrawOpacity removeLastObject]; [pathDrawLineWidth addObject:[bufferDrawLineWidth lastObject]]; [bufferDrawLineWidth removeLastObject]; [self setNeedsDisplay]; } } 

utiliser le nsundomanager, c'est le meilleur moyen d'annuler