我正在尝试创建一个应用程序,使用户签署一个特定区域,然后保存图像以将其发送到电子邮件中。但是我将图像传递给我的主视图控制器有点困难。这是我的代码:
#import "LinearInterpView.h"
@implementation LinearInterpView
{
UIBezierPath *path; // (3)
}
- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
if (self = [super initWithCoder:aDecoder])
{
[self setMultipleTouchEnabled:NO]; // (2)
[self setBackgroundColor:[UIColor whiteColor]];
path = [UIBezierPath bezierPath];
[path setLineWidth:2.0];
}
return self;
}
- (void)drawRect:(CGRect)rect // (5)
{
[[UIColor blackColor] setStroke];
[path stroke];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p]; // (4)
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end
当我使用Prepareforsegue方法时,它不起作用,因为viewcontroller没有再次加载。我想让用户在该区域签名,然后按下包含子视图的同一个viewcontroller上的按钮,并使用以下代码创建一个电子邮件:
[mailComposer addAttachmentData:imageData mimeType:@"image/png" fileName:@"Signature”];
如何在不先按某个按钮的情况下传递数据。非常感谢所有帮助!
答案 0 :(得分:0)
我想当你想绘制签名时,你只需添加一个UIView
。
因此,一种可行的方法是使用委托模式。
用几句话和一个简单的例子(我坚持“简单”,因为情况并非总是如此):
ObjectA创建ObjectB。
ObjectA拥有ObjectB,但ObjectB如何告诉ObjectA?代表们!
在 LinearInterpView.h 中,添加:
@class LinearInterpView;
@protocol LinearInterpViewDelegate < NSObject >
-(void)linearInterpView:(LinearInterpView *)linearInterp didRenderSignature:(UIImage *)signature;
@end
@property (nonatomic, weak) id < LinearInterpViewDelegate > delegate;
在 LinearInterpView.m 中,无论何时“准备就绪”(按钮点击或任何说明你签名后)
if ([self.delegate respondsToSelector:(linearInterpView:didRenderSignature:)])
[self.delegate linearInterpView:self didRenderSignature:[self imageWithView:self]];
在 YourUIViewController.h :
中@interface YourUIViewController : NSObject < LinearInterpViewDelegate>
在 YourUIViewController 中调用它时:
[yourLinearInterpView setDelegate:self];
在 YourUIViewController.m :
中-(void)linearInterpView:(LinearInterpView *)linearInterp didRenderSignature:(UIImage *)signature
{
[linearInterp removeFromSuperview];
//Do whatever you want with signature
}