iOS 上有触摸画线的 SDK 吗?

发布于 2024-11-26 01:28:13 字数 95 浏览 1 评论 0原文

有没有一个框架可以用来通过触摸画线。基本上,我想为客户添加在 iPad/iPhone 上签名并将其捕获为图像的功能。

非常感谢任何帮助。

谢谢。

Is there a framework I can use to draw lines with touch. Basically I want to add ability for a customer to sign on iPad/iPhone and capture it as an image.

Any help much appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2024-12-03 01:28:13

您可以使用UIKIT框架中提供的核心图形来满足您的要求。

我的应用程序有类似的要求,但用途不同,如果需要,我可以为您提供代码。

TNQ

.h 文件

#import <UIKit/UIKit.h>
typedef enum _DrawingMode{
DrawingModePen =0,
DrawingModeEraser=1,
} DrawingMode;
@interface DrawingView : UIView {
CGPoint lastPoint;
UIImageView *drawImage;
BOOL mouseSwiped;   
int mouseMoved;
DrawingMode mode;
UIColor *_drawingPenColor;
}
@property (nonatomic, retain) UIColor *drawingPenColor;
@property (nonatomic) DrawingMode mode;

@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic,retain)UIImageView *drawImage;
@end

.m

#import "DrawingView.h"


@implementation DrawingView

@synthesize mode, drawingPenColor=_drawingPenColor, imageView=drawImage;


-(void)initialize{
    drawImage = [[UIImageView alloc] initWithImage:nil];
    self.autoresizesSubviews = YES;
    drawImage.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    drawImage.frame = self.bounds;
    [self addSubview:drawImage];
    self.backgroundColor = [UIColor clearColor];
    mouseMoved = 0;
    _drawingPenColor = [[UIColor alloc] initWithWhite:0.0 alpha:1.0];
}

-(void)maskImage:(UIImage *)image withMask:(UIImage *)maskImage 
{
    CGImageRef maskRef = maskImage.CGImage; 
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                        CGImageGetHeight(maskRef),
                                        CGImageGetBitsPerComponent(maskRef),
                                        CGImageGetBitsPerPixel(maskRef),
                                        CGImageGetBytesPerRow(maskRef),
                                        CGImageGetDataProvider(maskRef), NULL, false);
    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
    UIImage *tempImage = [[UIImage alloc] initWithCGImage:masked];
    //self.clippedImage = tempImage;
    [tempImage release];
    CFRelease(masked);
    CFRelease(mask);
}

-(void)awakeFromNib{
    [self initialize];
}

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        [self initialize];
    }
    return self;
}

- (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];
    //lastPoint.y -= 20;
    NSLog(@"touches begin");

}

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

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


    UIGraphicsBeginImageContext(self.bounds.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    if (mode == DrawingModePen) {
        NSLog(@"drawing");
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [_drawingPenColor CGColor]);
    }
    else {
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]);

        CGContextBeginPath(UIGraphicsGetCurrentContext());


        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 20, 20));
        CGContextStrokePath(UIGraphicsGetCurrentContext());

        NSLog(@"clearing");
            }
    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.bounds.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        if (mode == DrawingModePen) {
            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [_drawingPenColor CGColor]);
        }
        else {
            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [self.backgroundColor CGColor]);
        }
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

- (void)dealloc {
    [drawImage release];
    [_drawingPenColor release];
    [super dealloc];
}

@end

you can meet your requirement by using core graphics available in UIKIT framework.

i had a similar requirement in my application with a different use ,if needed i can provide you the code.

TNQ

.h file

#import <UIKit/UIKit.h>
typedef enum _DrawingMode{
DrawingModePen =0,
DrawingModeEraser=1,
} DrawingMode;
@interface DrawingView : UIView {
CGPoint lastPoint;
UIImageView *drawImage;
BOOL mouseSwiped;   
int mouseMoved;
DrawingMode mode;
UIColor *_drawingPenColor;
}
@property (nonatomic, retain) UIColor *drawingPenColor;
@property (nonatomic) DrawingMode mode;

@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic,retain)UIImageView *drawImage;
@end

.m

#import "DrawingView.h"


@implementation DrawingView

@synthesize mode, drawingPenColor=_drawingPenColor, imageView=drawImage;


-(void)initialize{
    drawImage = [[UIImageView alloc] initWithImage:nil];
    self.autoresizesSubviews = YES;
    drawImage.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    drawImage.frame = self.bounds;
    [self addSubview:drawImage];
    self.backgroundColor = [UIColor clearColor];
    mouseMoved = 0;
    _drawingPenColor = [[UIColor alloc] initWithWhite:0.0 alpha:1.0];
}

-(void)maskImage:(UIImage *)image withMask:(UIImage *)maskImage 
{
    CGImageRef maskRef = maskImage.CGImage; 
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                        CGImageGetHeight(maskRef),
                                        CGImageGetBitsPerComponent(maskRef),
                                        CGImageGetBitsPerPixel(maskRef),
                                        CGImageGetBytesPerRow(maskRef),
                                        CGImageGetDataProvider(maskRef), NULL, false);
    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
    UIImage *tempImage = [[UIImage alloc] initWithCGImage:masked];
    //self.clippedImage = tempImage;
    [tempImage release];
    CFRelease(masked);
    CFRelease(mask);
}

-(void)awakeFromNib{
    [self initialize];
}

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        [self initialize];
    }
    return self;
}

- (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];
    //lastPoint.y -= 20;
    NSLog(@"touches begin");

}

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

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


    UIGraphicsBeginImageContext(self.bounds.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    if (mode == DrawingModePen) {
        NSLog(@"drawing");
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [_drawingPenColor CGColor]);
    }
    else {
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]);

        CGContextBeginPath(UIGraphicsGetCurrentContext());


        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 20, 20));
        CGContextStrokePath(UIGraphicsGetCurrentContext());

        NSLog(@"clearing");
            }
    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.bounds.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        if (mode == DrawingModePen) {
            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [_drawingPenColor CGColor]);
        }
        else {
            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [self.backgroundColor CGColor]);
        }
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

- (void)dealloc {
    [drawImage release];
    [_drawingPenColor release];
    [super dealloc];
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文