捕获陷在DrawRect方法中的UIView的UIImage [英] Capture UIImage of UIView stuck in DrawRect method

查看:101
本文介绍了捕获陷在DrawRect方法中的UIView的UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试捕获UIView的图像,但是当我调用此方法时

I am trying to capture image of my UIView but when I call this method

- (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;
}

我的应用程序进入了我的视图的DrawRect方法,并陷入了这些行的循环中

my application enter in DrawRect method of my view and stuck in loop of these lines

[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图像只需运行这些线和检查:

For capturing UIView image simply run these lines & check:

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

这篇关于捕获陷在DrawRect方法中的UIView的UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆