Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

iphone - get the UIImage (or CGImage) from 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);
}

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.

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.

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.

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 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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...