2013-04-03 30 views
1

我在iOS着色的图像绘制效率。我正在使用滑块来选择颜色。的iOS - (对滑块变化更新)

当我有设定为连续滑块“更新活动”,被调用该函数获取调用了很多(滑块从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像素,因为我希望能够放大而不损失质量。也许这是问题。我会继续修补,看看我能找到什么。

+0

您是否尝试过CA TiledLayer的? –

+0

你说得对,图像尺寸是一个因素,你是正对着的图像尺寸的无极限,正如大卫指出,平铺它。你需要在改变颜色的同时具有缩放功能吗?你可以做一个小副本,预览上的颜色变化那么一旦用户完成申请换到较大的图像,也许在后台? – Cocoadelica

回答

1

我不确定你的染色方法,但是我经历了一个类似的滑块性能问题,只是在一小段延迟后(即用户暂停滑动)调用性能密集型方法。

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

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

在你的方法挂在UISlider事件:在您的checkIfImageShouldBeColoured:方法

- (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]; 
    } 
} 

然后你可以看到,如果该值在此期间发生了变化:

- (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]; 
    } 
} 
+0

如果我正确阅读,你会安排很多定时器;) –

+0

*咳嗽*呃,是的。也许更好地使用一个计时器并检查时间戳值:) –