Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to take a guess on some things that were left out of the initial question.</p> <p>1) To clarify, the problem is: movement of the slider is not smooth. 2) Also, as a result of, or in combination with, this UI roughness, there is a delay in change to the image.</p> <p>I'm not sure what your implementation looks like, but, it sounds like you're doing too much work and/or too much on the main thread.</p> <p>So, heres what a functioning implementation might do:</p> <pre><code>- (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; } } </code></pre> <p>adjustImageBrightnessAsyncWithValue takes a value, original image, and completion block. It creates an operation (via NSOperation or NSOperationQueue, probably both) and keeps track of that operation. The operation applies the algorithm to the original image in the background. Once its done, the completion block sets the final image in your image view, on the main thread.</p> <p>The only things that happen on the main thread are: get the slider changed callback, canceling previous work, starting new work, and setting the final image. Everything else should happen in the background. Canceling work is an optimization for the case that the user moves the slider too fast for the image to be modified before the value changes again. Once the slider doesn't change long enough to do the modification, the result will be visible. The slider should always be smooth because nothing is blocking the main thread.</p> <p>EDIT</p> <p>Using an operation queue... Declare a member variable:</p> <pre><code>NSOperationQueue *m_queue; </code></pre> <p>...</p> <p>Initialize it in an init method:</p> <pre><code>m_queue = [NSOperationQueue new]; m_queue.maxConcurrentOperationCount = 1; // So it only does one brightness calculation at a time and there are no concurrency issues. </code></pre> <p>...</p> <pre><code>- (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; }); }]; } </code></pre> <p>Also, as an aside, you might take a look at GPUImage, discussed here: <a href="http://nshipster.com/gpuimage/" rel="nofollow">http://nshipster.com/gpuimage/</a> for numerous fast, powerful image modification techniques/APIs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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