1. 시작하기에 앞서 간단하게 화면을 보여줄 .xlb 파일에 UIImageView와 버튼 하나를 만들고 적당한 위치에 둔뒤 저장을한다.


2. 그다음 .h 파일루 간후 다음 과 같은 코드를 작성한다.

 
#import 

@interface what : UIViewController {
    UIImageView *drawImage;
    BOOL mouseMoved;
    BOOL mouseSwiped;
    CGPoint lastPoint;
    CGPoint currentPoint;
    IBOutlet UIImageView * card;
}

@property (nonatomic, retain) UIWebView *web;
@property (nonatomic, retain) UIImageView * card;

@end

3. .h 파일의 입력이 완료 되었다면 다음 .m 파일로 가서 나머지 부분을 다음과 같이 작성한다.

#import "what.h"
@implementation what
@synthesize web;
@synthesize card;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    drawImage = [[UIImageView alloc] initWithImage:nil];
    drawImage.frame = [card frame];
    [self.view addSubview:drawImage];
    self.view.backgroundColor = [UIColor whiteColor];
    mouseMoved = 0;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
   //  더블터치시 낙서된 걸 지워주는 역활을 한다.
    if ([touch tapCount] == 2) {
        drawImage.image = nil;
        return;
    }
    lastPoint = [touch locationInView:self.card];
    lastPoint.y -= 0;
}

// 터치 이벤트
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];   
// touch locationInView:self.card 라고 되어있는데 이건 UIImageView를 가리키는 부분이다.
    currentPoint = [touch locationInView:self.card];  
    currentPoint.y -= 0;
    UIGraphicsBeginImageContext(card.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, card.frame.size.width, card.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    // 아래 한줄은  drawing시 펜 색깔을 지정해주는 부분이다 뒤에 숫자는 ( 레드, 그린, 블루, 알파(투명) ) 을 나타낸다
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    lastPoint = currentPoint;
    mouseMoved++;
    if (mouseMoved == 10) {
        mouseMoved = 0;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch tapCount] == 2) {
        drawImage.image = nil;
        return;
    }
    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
@end



4. 다시 .xlb 파일로 가서 imageview를 card 아울렛과 연결한후 실행하고난뒤 이미지뷰에 드로잉을하면 성공!


위와 같이 UIImageView 위에서만 낙서가 되는걸 볼수가 있다.



# .m 파일에서 터치 이벤트 부분을 자세히 살펴보면 다음과 같은 if문이 있다. 

if ([touch tapCount] == 2) {
        drawImage.image = nil;
        return;
}

 이부분은 drawing해서 지저분해진걸 더블클릭으로 깨끗하게 삭제시켜주는 기능이다.

역시 이것또한 Button을 만들어서 Button Action기능을 넣어서 Button 클릭시 

drawImage.image = nil;

이라는 값만 넣어주면 Button 클릭으로 화면에 drawing한걸 초기화시켜 지워 줄수 있도록 수정이 가능하다.