Iphone 捕获UIView的UIImage卡在DrawRect方法中
我试图捕获UIView的图像,但当我调用此方法时Iphone 捕获UIView的UIImage卡在DrawRect方法中,iphone,ios,uiview,uiimage,drawrect,Iphone,Ios,Uiview,Uiimage,Drawrect,我试图捕获UIView的图像,但当我调用此方法时 - (UIImage *) imageWithView:(UIView *)view { UIGraphicsBeginImageContext(view.bounds.size); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
- (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
我的应用程序输入视图的DrawRect方法,并卡在这些行的循环中
[super drawRect:rect];
[curImage drawAtPoint:CGPointMake(0, 0)];
mid1 = midPoint(previousPoint1, previousPoint2);
mid2 = midPoint(currentPoint, previousPoint1);
context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
这是我的drawRect
方法
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
[curImage drawAtPoint:CGPointMake(0, 0)];
mid1 = midPoint(previousPoint1, previousPoint2);
mid2 = midPoint(currentPoint, previousPoint1);
context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextStrokePath(context);
}
要捕获UIView图像,只需运行以下行并选中:
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
您还可以将图像保存在文档目录中并捕获UIView图像
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
//inserttrue=TRUE;
UIGraphicsEndImageContext();
imagdata = UIImagePNGRepresentation(saveImage);
NSFileManager *fileMan = [NSFileManager defaultManager];
[self.view setContentStretch:CGRectMake(0, 0, 320, 480)];
NSString *fileName = [NSString stringWithFormat:@"%@.png",@"XYZ"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[fileMan createFileAtPath:pdfFileName contents:imagdata attributes:nil];
是否调用了drow rect方法?@NitinGohel thanx以获取答复。DrawRect正在一次又一次地调用infect。现在vishal解决了我的问题Thanx:)Thanx也给你…享受。vishal它正在工作,但它一次又一次地调用我的SignatureView的drawRect方法。它被困在一个永远不会结束的循环中,您对drawRect方法做了什么?这意味着您为什么调用drawRect方法?