[英]how to convert the part of UIView into UIImage and display it in imageView
[英]how to convert UIView part as UIImage in iphone application
我有一个应用程序,在该应用程序中,我正在用手指从用户那里获得签名,但效果很好,但我希望将签名转换为图像,并在imageview中显示,这是我的签名代码。
签名工作正常,它显示在视图上,但我想将这些图形行转换为图像并在imageview中显示。
#import <UIKit/UIKit.h>
@interface MyLineDrawingView : UIView {
UIBezierPath *myPath;
UIColor *brushPattern;
}
@end
实现类
#import "MyLineDrawingView.h"
@implementation MyLineDrawingView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor=[UIColor whiteColor];
myPath=[[UIBezierPath alloc]init];
myPath.lineCapStyle=kCGLineCapRound;
myPath.miterLimit=0;
myPath.lineWidth=10;
brushPattern=[UIColor redColor];
}
return self;
}
//仅重写drawRect:如果执行自定义绘制。 //空的实现会对动画期间的性能产生不利影响。
- (void)drawRect:(CGRect)rect
{
[brushPattern setStroke];
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
// Drawing code
//[myPath stroke];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)dealloc
{
[brushPattern release];
[super dealloc];
}
@end
这就是我所说的观点
signatureView=[[UIView alloc] initWithFrame:CGRectMake(100,100,800,500)];
signatureView.backgroundColor=[UIColor blackColor];
[self.view addSubview:signatureView];
UIButton*OkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[OkButton setFrame:CGRectMake(100,420,200,40)];
[OkButton setTitle:@"OK" forState:UIControlStateNormal];
[OkButton addTarget:self action:@selector(onOKButtonClick) forControlEvents:UIControlEventTouchUpInside];
[signatureView addSubview:OkButton];
MyLineDrawingView *drawScreen=[[MyLineDrawingView alloc]initWithFrame:CGRectMake(10,10,700,400)];
[signatureView addSubview:drawScreen];
[drawScreen release];
尝试获取MyLineDrawingView的屏幕截图:
- (UIImage*)takeSignatureImage{
UIGraphicsBeginImageContext(drawScreen.bounds.size);
[drawScreen.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *signatureImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return signatureImage;
}
-(IBAction)onOKButtonClick{
UIImage *signatureImg=[self takeSignatureIamge];
if(signatureImg){
yourImageView.image=signatureImg;
}
}
确保将drawScreen
保留为实例变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.