Drawing incrementally in a UIView (iPhone)

后端 未结 5 1454
甜味超标
甜味超标 2020-12-02 14:09

As far as I have understood so far, every time I draw something in the drawRect: of a UIView, the whole context is erased and then redrawn.

So I have to do something

5条回答
  •  难免孤独
    2020-12-02 14:35

    How many ellipses are you going to draw? In general, Core Graphics should be able to draw a lot of ellipses quickly.

    You could, however, cache your old drawings to an image (I don't know if this solution is more performant, however):

    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext(); // ctx is now the image's context
    
    [cachedImage drawAtPoint:CGPointZero];
    // only plot new ellipses here...
    
    [cachedImage release];
    cachedImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
    UIGraphicsEndImageContext();
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextDrawImage(context, self.bounds, maskRef);          //draw the mask
    CGContextClipToMask(context, self.bounds, maskRef);         //respect alpha mask
    CGContextSetBlendMode(context, kCGBlendModeColorBurn);      //set blending mode
    
    [cachedImage drawAtPoint:CGPointZero];
    

提交回复
热议问题