从drawRect获取UIImage(或CGImage) [英] get the UIImage (or CGImage) from drawRect

查看:58
本文介绍了从drawRect获取UIImage(或CGImage)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从drawRect调用以下代码

I'm calling the following code from drawRect


- (void) drawPartial:(UIImage *)img colour:(UIColor *)colour  {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextDrawImage(context, self.frame, img.CGImage);

    if (colour!=nil) {
        CGContextSetBlendMode (context, kCGBlendModeColor ); 
        CGContextClipToMask(context, self.bounds, img.CGImage); 
        CGContextSetFillColor(context, CGColorGetComponents(colour.CGColor));
        CGContextFillRect (context, self.bounds);
    }

    self.currentImage=UIGraphicsGetImageFromCurrentImageContext();
    CGContextRestoreGState(context);
}

事实上,我多次调用它来构建动态着色的合成图像。传入的img实际上是透明的覆盖层,上面有图像的不同部分。

Infact I call it multiple times to build a composite image dynamically coloured in. Each img passed in is effectively a transparent overlay with a different part of the image on it.

我可以通过在drawrect中多次调用此复合图像来构建复合图像。当我要更新图像的一部分时,就会出现问题。理想情况下,我可以调用上面的函数并只更改其中一部分:我尝试过使用self.clearsContextBeforeDrawing无济于事。

I can build my compound image by calling this multiple times in drawrect. The issue comes when I want to update one part of the image. Ideally I would be able to call the above function and just change the one part: I've tried playing with self.clearsContextBeforeDrawing to no avail.

然后我想我会尝试从每次绘制中保留图像的副本,作为状态的缓存图像-这样,我只需要在其上覆盖新的部分:两个调用而不是15。

Then I thought I would try to keep a copy of the image from each draw, as a cached image of the state - that way I just need to overlay the new part on that: two calls rather than 15.

但是那行self.currentImage = UIGraphicsGetImageFromCurrentImageContext()并没有返回我当前的图像,因此我无法构建缓存的副本。

However the line self.currentImage=UIGraphicsGetImageFromCurrentImageContext() is not returning me the current image so I can't build a cached copy.

任何一种帮助方法将不胜感激。 (或者显然为我指明了我应该这样做的方式!)

Any help on either approach would really be appreciated. (or obviously point me the way I should be doing this!)

EDIT
我还尝试使用几乎相同的代码分别合成图像,然后绘制那,但是我再也没有得到图像...

EDIT I also tried compositing the image separtly using virtually the same code, and then drawing that, but again I don't get the image out...


- (UIImage *) overlayImage:(UIImage *)srcImage withImage:(UIImage *)overlayImage ofColour:(UIColor *)colour   {

    CGSize size =srcImage.size;
    CGRect box = CGRectMake(0, 0, size.width, size.height);

    UIGraphicsBeginImageContext(size);
    CGContextRef context=UIGraphicsGetCurrentContext();

    CGContextTranslateCTM(context, 0.0, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextDrawImage(context, box, srcImage.CGImage);


    if (colour!=nil) {
        CGContextSetBlendMode (context, kCGBlendModeColor ); //kCGBlendModeMultiply
        CGContextClipToMask(context, box, overlayImage.CGImage); // respect alpha mask
        CGContextSetFillColor(context, CGColorGetComponents(colour.CGColor));
        CGContextFillRect (context, box);
    }

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    //UIImage *result = [UIImage imageWithCGImage: CGBitmapContextCreateImage (context)];
    UIGraphicsEndImageContext();
    return result;
}


推荐答案

通常,您要 -drawRect:尽可能简单。

As a rule, you want -drawRect: to be as simple as possible.

将绘图代码从 -drawRect:移至创建并绘制所需整个图像的方法中。减少 -drawRect:只是将该图像合成到当前上下文中。

Move your drawing code from -drawRect: into a method that creates and draws the entire image you want. Reduce -drawRect: to just compositing this image into the current context.

这篇关于从drawRect获取UIImage(或CGImage)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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