Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS - Drawing efficiency (update on slider change)
    text
    copied!<p>I am colouring an image on iOS. I am using a slider to select the colour.</p> <p>When I have "Update Events" on the slider set to continuous, the function that gets called is getting called a lot (the slider goes from 0 to 1535) and so the user interface isn't very responsive.</p> <p>Is there a way I can make the following code more efficient? I realise I'm starting a new drawing context every time I call the function - can I "save" this context and re-use it?</p> <p>Thanks in advance.</p> <pre><code>- (IBAction)bodyColourChanged:(UISlider *)sender { // get the UIColor from self.colourArray UIColor *color = [self.colourArray objectAtIndex:sender.value]; UIImage *myImage = [UIImage imageNamed:@"body.png"]; // Begin a new image context to draw the coloured image onto UIGraphicsBeginImageContext(self.bodyView.image.size); // Get a reference to the context we created CGContextRef context = UIGraphicsGetCurrentContext(); // Set the fill colour //[[UIColor colorWithRed:color.CGColor green:green blue:blue alpha:1.0] setFill]; [color setFill]; // translate/flip the graphics context (for transforming from CG* coords to UI* coords CGContextTranslateCTM(context, 0, self.bodyView.image.size.height); CGContextScaleCTM(context, 1.0, -1.0); // set the blend mode and the original image CGContextSetBlendMode(context, kCGBlendModeOverlay); CGRect rect = CGRectMake(0, 0, self.bodyView.image.size.width, self.bodyView.image.size.height); CGContextDrawImage(context, rect, myImage.CGImage); // Set a mask that matches the shape of the image, then draw (colour burn) a coloured rectangle CGContextClipToMask(context, rect, self.bodyView.image.CGImage); CGContextAddRect(context, rect); CGContextDrawPath(context, kCGPathFill); // Generate a new UIImage from the graphics context we drew onto UIImage *colouredImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); self.bodyView.image = colouredImage; } </code></pre> <p>EDIT: The image I'm colouring is quite large. It's 1541 x 2000 pixels because I want to be able to zoom in without loss of quality. Maybe this is the issue. I'll keep tinkering to see what I can find out.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload