iOS 手寫簽名的簡單實(shí)用封裝

簡介

現(xiàn)在很多項(xiàng)目在完善信息或者注冊信息的時(shí)候,或者支付這一方面,都希望用戶手寫簽名,這樣既可以保證是用戶親自簽名的,保證該記錄是用用戶操作的,而不是別人操作的.所以手寫簽字這個(gè)還是比較重要的.下面就是通過QuartzCore來繪制簽名.QuartzCore是iOS的核心動畫框架.

繪制

1定義一個(gè)結(jié)構(gòu)體

1

2

3

4

5

6

staticCGPoint?midpoint(CGPoint?p0,CGPoint?p1)?{

return(CGPoint)?{

(p0.x?+?p1.x)?/2.0,

(p0.y?+?p1.y)?/2.0

};

}

2添加手勢

UIPanGestureRecognizer?*pan?=?[[UIPanGestureRecognizer?alloc]?initWithTarget:self?action:@selector(pan:)];

pan.maximumNumberOfTouches?=?pan.minimumNumberOfTouches?=1;

[self?addGestureRecognizer:pan];

3開始繪制

CGPoint?currentPoint?=?[pan?locationInView:self];

CGPoint?midPoint?=?midpoint(previousPoint,?currentPoint);

NSLog(@"獲取到的觸摸點(diǎn)的位置為--currentPoint:%@",NSStringFromCGPoint(currentPoint));

[self.currentPointArr?addObject:[NSValue?valueWithCGPoint:currentPoint]];

self.hasSignatureImg?=?YES;

CGFloat?viewHeight?=?self.frame.size.height;

CGFloat?currentY?=?currentPoint.y;

if(pan.state?==UIGestureRecognizerStateBegan)?{

[path?moveToPoint:currentPoint];

}elseif(pan.state?==UIGestureRecognizerStateChanged)?{

[path?addQuadCurveToPoint:midPoint?controlPoint:previousPoint];

}

if(0<=?currentY?&&?currentY?<=?viewHeight)

{

if(max?==0&&min?==0)

{

max?=?currentPoint.x;

min?=?currentPoint.x;

}

else

{

if(max?<=?currentPoint.x)

{

max?=?currentPoint.x;

}

if(min>=currentPoint.x)

{

min?=?currentPoint.x;

}

}

}

previousPoint?=?currentPoint;

//記得調(diào)用,及時(shí)刷新視圖

[self?setNeedsDisplay];

4獲取繪制視圖,在進(jìn)行一系列處理就好

if(UIGraphicsBeginImageContextWithOptions?!=NULL)

{

UIGraphicsBeginImageContextWithOptions(self.bounds.size,NO,?[UIScreen?mainScreen].scale);

}else{

UIGraphicsBeginImageContext(self.bounds.size);

}

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

UIImage?*image?=UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//繪制成圖

image?=?[self?imageBlackToTransparent:image];

NSLog(@"width:%f,height:%f",image.size.width,image.size.height);

//截取圖片

UIImage?*img?=?[self?cutImage:image];

//壓縮圖片

self.SignatureImg?=?[self?scaleToSize:img];

5附上處理的方法

1.繪制成圖

-?(UIImage*)?imageBlackToTransparent:(UIImage*)?image

{

//?分配內(nèi)存

constintimageWidth?=?image.size.width;

constintimageHeight?=?image.size.height;

size_t??????bytesPerRow?=?imageWidth?*4;

uint32_t*?rgbImageBuf?=?(uint32_t*)malloc(bytesPerRow?*?imageHeight);

//?創(chuàng)建context

CGColorSpaceRef?colorSpace?=CGColorSpaceCreateDeviceRGB();

CGContextRef?context?=?CGBitmapContextCreate(rgbImageBuf,?imageWidth,?imageHeight,8,?bytesPerRow,?colorSpace,

kCGBitmapByteOrder32Little?|?kCGImageAlphaNoneSkipLast);

CGContextDrawImage(context,?CGRectMake(0,0,?imageWidth,?imageHeight),?image.CGImage);

//?遍歷像素

intpixelNum?=?imageWidth?*?imageHeight;

uint32_t*?pCurPtr?=?rgbImageBuf;

for(inti?=0;?i?<?pixelNum;?i++,?pCurPtr++)

{

//????????if?((*pCurPtr?&?0xFFFFFF00)?==?0)????//將黑色變成透明

if(*pCurPtr?==0xffffff)

{

uint8_t*?ptr?=?(uint8_t*)pCurPtr;

ptr[0]?=0;

}

//改成下面的代碼,會將圖片轉(zhuǎn)成灰度

/*uint8_t*?ptr?=?(uint8_t*)pCurPtr;

//?gray?=?red?*?0.11?+?green?*?0.59?+?blue?*?0.30

uint8_t?gray?=?ptr[3]?*?0.11?+?ptr[2]?*?0.59?+?ptr[1]?*?0.30;

ptr[3]?=?gray;

ptr[2]?=?gray;

ptr[1]?=?gray;*/

}

//?將內(nèi)存轉(zhuǎn)成image

CGDataProviderRef?dataProvider?=?CGDataProviderCreateWithData(NULL,?rgbImageBuf,?bytesPerRow?*?imageHeight,/*ProviderReleaseData**/NULL);

CGImageRef?imageRef?=?CGImageCreate(imageWidth,?imageHeight,8,32,?bytesPerRow,?colorSpace,

kCGImageAlphaLast?|?kCGBitmapByteOrder32Little,?dataProvider,

NULL,true,kCGRenderingIntentDefault);

CGDataProviderRelease(dataProvider);

UIImage*?resultUIImage?=?[UIImage?imageWithCGImage:imageRef];

//?釋放

CGImageRelease(imageRef);

CGContextRelease(context);

CGColorSpaceRelease(colorSpace);

//?free(rgbImageBuf)?創(chuàng)建dataProvider時(shí)已提供釋放函數(shù),這里不用free

returnresultUIImage;

}

2.截圖圖片

CGRect?rect?;

//簽名事件沒有發(fā)生

if(min?==0&&max?==0)

{

rect?=CGRectMake(0,0,0,0);

}

else//簽名發(fā)生

{

rect?=CGRectMake(min-3,0,?max-min+6,self.frame.size.height);

}

CGImageRef?imageRef?=CGImageCreateWithImageInRect([image?CGImage],?rect);

UIImage?*?img?=?[UIImage?imageWithCGImage:imageRef];

//添加水印

UIImage?*lastImage?=?[self?addText:img?text:self.showMessage];

CGImageRelease(imageRef);

[self?setNeedsDisplay];

3.壓縮

//壓縮圖片,最長邊為128(根據(jù)不同的比例來壓縮)

-?(UIImage?*)scaleToSize:(UIImage?*)img?{

CGRect?rect?;

CGFloat?imageWidth?=?img.size.width;

//判斷圖片寬度

if(imageWidth?>=128)

{

rect?=CGRectMake(0,0,128,?self.frame.size.height);

}

else

{

rect?=CGRectMake(0,0,?img.size.width,self.frame.size.height);

}

CGSize?size?=?rect.size;

UIGraphicsBeginImageContext(size);

[img?drawInRect:rect];

UIImage*?scaledImage?=UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//此處注釋是為了防止該簽名圖片被保存到本地

//????UIImageWriteToSavedPhotosAlbum(scaledImage,nil,?nil,?nil);

[self?setNeedsDisplay];

returnscaledImage;

}

剩下的,都是一些細(xì)節(jié)問題,根據(jù)不同的項(xiàng)目進(jìn)行不同的修改就好.

自己做的demo效果

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容