iOS - 绘图效率(更改滑块更改)

时间:2013-04-03 14:04:40

标签: iphone ios ipad cgcontext

我在iOS上着色图像。我正在使用滑块来选择颜色。

当滑块上的“Update Events”设置为连续时,被调用的函数被调用很多(滑块从0到1535),因此用户界面响应不是很快。

有没有办法让下面的代码更有效率?我意识到每次调用函数时我都会启动一个新的绘图上下文 - 我可以“保存”这个上下文并重新使用它吗?

提前致谢。

- (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;
}
编辑:我着色的图像非常大。这是1541 x 2000像素,因为我希望能够放大而不会降低质量。也许这就是问题所在。我会继续修补我能找到的东西。

1 个答案:

答案 0 :(得分:1)

我不确定你的着色方法,但我通过在一小段延迟后调用性能密集型方法(即用户暂停滑动)来解决滑块的类似性能问题。

创建一些类变量/属性来保存与时间相关的对象:

@property (nonatomic, strong) NSDate *sliderValueChangedDate;
@property (nonatomic, strong) NSTimer *sliderValueChangedTimer;

在您的方法中连接UISlider事件:

- (IBAction)sliderValueChanged:(id)sender {
    // Save the time the slider was changed.
    self.sliderValueChangedDate = [NSDate date];
    // Start a timer if it's not already running.
    if (!self.sliderValueChangedTimer) {
        self.sliderValueChangedTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(checkIfImageShouldBeColoured:) userInfo:nil repeats:YES];
    }
}

然后在checkIfImageShouldBeColoured:方法中,您可以看到临时值是否发生了变化:

- (void)checkIfImageShouldBeColoured:(NSTimer *)timer {
    // Get how long has been elapsed since the slider was last changed.
    NSTimeInterval elapsed = -[self.sliderValueChangedDate timeIntervalSinceNow];
    // If this is over our threshold, then perform the intensive method.
    if (elapsed > 0.3) {
        [self.sliderValueChangedTimer invalidate];
        self.sliderValueChangedTimer = nil;
        [self changeBodyColour];
    }
}