我将对最初问题中遗漏的一些内容进行猜测。
1)澄清一下,问题是:滑块的移动不顺畅。
2) 此外,由于或与此 UI 粗糙度相结合,图像的更改存在延迟。
我不确定您的实现是什么样的,但是,听起来您在主线程上做了太多工作和/或太多。
那么,下面是一个正常运行的实现可能会做什么:
- (void)sliderChanged:(UISlider *)sender
{
[self adjustImageBrightnessWithValue:sender.value;
}
- (void)adjustImageBrightnessWithValue:(CGFloat)value
{
[self cancelCurrentWork]; // Maintain a reference to an operation and cancel it
[self adjustImageBrightnessAsyncWithValue:(CGFloat)value originalImage:self.imageView.image completion:^(UIImage *finalImage)
{
self.imageView.image = finalImage;
}
}
adjustImageBrightnessAsyncWithValue 接受一个值、原始图像和完成块。它创建一个操作(通过 NSOperation 或 NSOperationQueue,可能两者都有)并跟踪该操作。该操作将算法应用于背景中的原始图像。完成后,完成块在主线程上的图像视图中设置最终图像。
在主线程上发生的唯一事情是:获取滑块更改回调、取消之前的工作、开始新的工作以及设置最终图像。其他一切都应该在后台发生。取消工作是针对用户移动滑块太快而无法在值再次更改之前修改图像的情况的优化。一旦滑块没有改变足够长的时间来进行修改,结果将是可见的。滑块应始终平滑,因为没有任何东西阻塞主线程。
编辑
使用操作队列...
声明一个成员变量:
NSOperationQueue *m_queue;
...
在 init 方法中初始化它:
m_queue = [NSOperationQueue new];
m_queue.maxConcurrentOperationCount = 1; // So it only does one brightness calculation at a time and there are no concurrency issues.
...
- (void)adjustImageBrightnessWithValue:(CGFloat)value
{
[m_queue cancelAllOperations];
[m_queue addOperationWithBlock:^
{
UIImage *adjustedImage = [mainimage brightness:value]; // Not sure where this method is coming from, but this code assumes it returns a copy of mainimage with the brightness adjusted.
dispatch_async(dispatch_get_main_queue(), ^
{
imageview.image = adjustedImage;
});
}];
}
另外,顺便说一句,您可以看看 GPUImage,在此讨论:http://nshipster.com/gpuimage/,了解众多快速、强大的图像修改技术/API。