2011-05-15 46 views
0

我试图从iPad的触摸屏上将指纹签名打印到PDF,然后将生成的PDF通过电子邮件发送到预设地址。我写了一个UIView子类,它在当前拖动事件的位置和最后一个拖动事件的位置之间添加了一些行,如下所示。我没有实施电子邮件部分的麻烦。子类的声明:在iOS中将子视图图像打印到PDF

#import <UIKit/UIKit.h> 

@interface SignatureView : UIView { 
    UIImageView *drawImage; 
    @public 
    UIImage *cachedSignature; 
} 
@property (nonatomic, retain) UIImage* cachedSignature; 
-(void) clearView; 

@end 

与实现

#import "SignatureView.h" 

@implementation SignatureView 

Boolean drawSignature=FALSE; 
float oldTouchX=-1; 
float oldTouchY=-1; 
float nowTouchX=-1; 
float nowTouchY=-1; 
@synthesize cachedSignature; 
//UIImage *cachedSignature=nil; 

- (id)initWithFrame:(CGRect)frame { 

    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code. 
    } 
    return self; 
    if (cachedSignature==nil){ 
     if(UIGraphicsBeginImageContextWithOptions!=NULL){ 
      UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0); 
     }else{ 
      UIGraphicsBeginImageContext(self.frame.size); 
     } 
     [self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     cachedSignature = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 
} 

- (void)drawRect:(CGRect)rect { 
    //get image of current state of signature field 
    [self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    cachedSignature = UIGraphicsGetImageFromCurrentImageContext(); 

    //draw cached signature onto signature field, no matter what. 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextDrawImage(context, CGRectMake(0,0,302,90),cachedSignature.CGImage); 

    if(oldTouchX>0 && oldTouchY>0 && nowTouchX>0 && nowTouchY>0){ 
     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
     CGContextSetLineWidth(context, 2); 
       //make change to signature field 
     CGContextMoveToPoint(context, oldTouchX, oldTouchY); 

     CGContextAddLineToPoint(context, nowTouchX, nowTouchY); 
     CGContextStrokePath(context); 
    } 
} 
-(void) clearView{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetGrayFillColor(context, 0.75, 1.0); 
    CGContextFillRect(context, CGRectMake(0,0,800,600)); 
    CGContextFlush(context); 
    cachedSignature = UIGraphicsGetImageFromCurrentImageContext(); 
    [self setNeedsDisplay]; 
} 

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    CGPoint location=[[touches anyObject] locationInView:self]; 
    oldTouchX=nowTouchX; 
    oldTouchY=nowTouchY; 
    nowTouchX=location.x; 
    nowTouchY=location.y; 
    [self setNeedsDisplay]; 
} 
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    CGPoint location=[[touches anyObject] locationInView:self]; 
    oldTouchX=nowTouchX; 
    oldTouchY=nowTouchY; 
    nowTouchX=location.x; 
    nowTouchY=location.y; 
    [self setNeedsDisplay]; 
} 

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    oldTouchX=-1; 
    oldTouchY=-1; 
    nowTouchX=-1; 
    nowTouchY=-1; 
} 

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


@end 

我,但是,有麻烦打印签名的PDF。据我了解上面的代码,它应该保留一个签名的副本,作为名为cachedSignature的UIImage的最后一步,所有人都可以访问。但是,当我尝试使用以下内容将其写入PDF上下文时:

UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil); 
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil); 
    UIImage* background=[[UIImage alloc] initWithContentsOfFile:backgroundPath]; 
    // Create the PDF context using the default page size of 612 x 792. 
    // Mark the beginning of a new page. 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(context, 0.0, 792); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    //CGContextDrawImage(context, CGRectMake(0,0,612,790),background.CGImage); 
    //draw signature images 
    UIImage *y=clientSignatureView.cachedSignature; 
    CGContextDrawImage(context, CGRectMake(450, 653, 600, 75), y.CGImage); 
    //CGContextDrawImage(context, CGRectMake(75, 75, 300, 300),  techSignatureView.cachedSignature.CGImage); 
    CGContextTranslateCTM(context, 0.0, 792); 
    CGContextScaleCTM(context, 1.0, -1.0); 

它失败。在这个例子中,'techSignatureView'和'clientSignatureView'是上面定义的自定义UIView的实例,与运行此代码的UIView在同一个父级连接到插座。

我不知道发生了什么问题。我已经拿出了打印背景图片的电话,以防他们被打印在“背后”,而没有结果。在上面的代码中使用调试器检查'y'表明它具有零协议条目和零方法条目;所以我怀疑它没有被正确访问 - 除此之外,我很无知,不知道如何继续。

回答

0

首先,当您尝试绘制它时,您的cachedSignature可能不再存在,因为它是自动释放的。当你分配它时使用属性设置器,所以不要使用cachedSignature = foo来编写self.cachedSignature = foo

另外,UIGraphicsGetImageFromCurrentImageContext仅适用于图像上下文,不能在当前上下文中使用drawRect:。方法drawRect:负责将视图绘制到屏幕上,但不能从相同的上下文创建图像,因此必须使用UIGraphicsBeginImageContext来创建将图像绘制到的新上下文。无论如何,drawRect:方法并不是最好的地方,因为如果每次触摸移动时渲染图像,性能可能会很差。相反,我建议将签名呈现给touchesEnded:withEvent:实施中的图像。