如何创建一个允许用户自由手绘的应用程序?

时间:2011-11-21 06:07:32

标签: iphone ipad

我是iPad应用程序开发的新手。

我有开发像iDesk应用程序这样的应用程序的任务我不知道如何从应用程序开始。

此应用程序有1.徒手绘图2.形状识别&更多功能。但我不知道如何从这个应用程序开始。 请帮帮我。

提供有关如何创建此应用的一些详细信息。我认为这个应用程序是从openGL创建的。如果可能,请提供一些样品。

请帮帮我。

我发布了有关此主题的新问题New Question for this topic

请帮助我。

2 个答案:

答案 0 :(得分:4)

这是我用于绘图的UIView子类。我不记得我在哪里采购它所以如果有人认出代码,请给予原始程序员:

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h> 



 @interface SignatureView : UIView {
       UIImage *myPic;
   }

   @property (strong, nonatomic) NSMutableArray *myDrawing;
   @property (nonatomic) BOOL canDraw;

   - (void) drawPic:(UIImage*)thisPic;
   - (void) cancelDrawing;
   - (UIImage*) collectSignature;

   @end




    #import "SignatureView.h"

    @implementation SignatureView

    @synthesize canDraw, myDrawing;

    - (void)drawRect:(CGRect)rect 
    {
    //round the corners
    self.layer.cornerRadius = 5;
    self.clipsToBounds = YES;

    float newHeight;
    float newWidth;
    if (!myDrawing) 
        myDrawing = [[NSMutableArray alloc] initWithCapacity:0];

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    if (myPic != NULL) 
    {
        float 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;
        [myPic drawInRect:CGRectMake(0,0, newWidth, newHeight)];
    }
    if ([myDrawing count] > 0) 
    {
        CGContextSetLineWidth(ctx, 5);
        for (int i = 0; i < [myDrawing count]; i++) 
        {
            NSArray *thisArray = [myDrawing objectAtIndex:i];
            if ([thisArray count] > 2) 
            {
                float thisX = [[thisArray objectAtIndex:0] floatValue];
                float thisY = [[thisArray objectAtIndex:1] floatValue];
                CGContextBeginPath(ctx);
                CGContextMoveToPoint(ctx, thisX, thisY);
                for (int j = 2; j < [thisArray count] ; j+=2) 
                {
                    thisX = [[thisArray objectAtIndex:j] floatValue];
                    thisY = [[thisArray objectAtIndex:j+1] floatValue];
                    CGContextAddLineToPoint(ctx, thisX,thisY);
                }
                CGContextStrokePath(ctx);
            }
        }
    }
}

- (void)drawPic:(UIImage *)thisPic 
{
    myPic = thisPic;
    [self setNeedsDisplay];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]]; //memory leak
    CGPoint curPoint = [[touches anyObject] locationInView:self];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; //memory leak
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; //memory leak
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    CGPoint curPoint = [[touches anyObject] locationInView:self];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; //memory leak
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; //memory leak
    [self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    CGPoint curPoint = [[touches anyObject] locationInView:self];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
    [self setNeedsDisplay];
}

-(void)cancelDrawing 
{
    [myDrawing removeAllObjects];
    [self setNeedsDisplay];
}

- (UIImage *)collectSignature
{
    //take screenshot
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

@end

答案 1 :(得分:1)

使用以下我之前使用过的。它肯定会起作用: -

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

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        [drawImage setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"white" ofType:@"png"]]];
        return;
    }

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

}

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

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.drawImage];
    currentPoint.y -= 20;

    NSLog(@"current Point is x: %d, y: %d",currentPoint.x,currentPoint.y);

    UIGraphicsBeginImageContext(self.drawImage.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.drawImage.frame.size.width, self.drawImage.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

}