1

我想在 uiview 上绘制一个随机设计,就像我们在画笔上绘制我想要用户在屏幕上触摸开始绘制的位置一样。就像他想在里面写好而不是画出来。如果他想做鸭子或球,那么他就可以做到。请帮助我。

4

2 回答 2

0

你将不得不让你的 UIView 成为上述的子类。在您的控制器 class.m 文件中,您必须添加并调用这两个方法。我希望这行得通。

-(IBAction)清除{

[self.view cancelDrawing];

}

-(IBAction)saveDrawing {

UIGraphicsBeginImageContext(self.view.bounds.size);

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *finishedPic = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(finishedPic, self, @selector(exitProg:didFinishSavingWithError:contextInfo:), nil);

}

于 2012-05-24T10:00:18.383 回答
0

尝试这个。在此,您将不得不self.view.frame在我拥有的任何地方使用myPic.Frame

在头文件中:

#import <Foundation/Foundation.h>

@interface DrawView : UIView {

    UIImage *myPic;

    NSMutableArray *myDrawing;

}

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

-(void)cancelDrawing;

@end

并在实施文件中:

#import "DrawView.h"

@implementation DrawView

-(void)drawPic:(UIImage *)thisPic {

    myPic = thisPic;

    [myPic retain];

    [self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect {

    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) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]];

    CGPoint curPoint = [[touches anyObject] locationInView:self];

    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];

    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];

}

- (void) touchesMoved:(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) 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];

}

- (void)dealloc {

    [super dealloc];

    [myPic release];

    [myDrawing release];

}

@end
于 2012-05-22T04:33:58.163 回答