0

Pretty new to cocoa development and really stuck with probably a fundamental problem.

So in short, my app UI looks like a simple window with a nsslider at the bottom. What I need is to generate N images and place them, onto N nsviews in my app window.

What it does so far:

  • I'm clicking on the slider (holding it) and dragging it. While I'm dragging it nothing happens to my views (pictures are not generated). When I release the slider the pictures got generated and my view get filled with them.

What I want: - I need the views to be filled with pictures as I'm moving the slider.

I figured out the little check box on the NSSlider properties, which is continuous, and I'm using it, but my image generator still doesn't do anything until I release the slider.

Here is my code:

// slider move action
- (IBAction)sliderMove:(id)sender
{   
    [self generateProcess:[_slider floatValue];
}

// generation process
- (void) generateProcess:(Float64) startPoint
{
    // create an array of times for frames to display
    NSMutableArray *stops = [[NSMutableArray alloc] init];

    for (int j = 0; j < _numOfFramesToDisplay; j++)
    {
        CMTime time = CMTimeMakeWithSeconds(startPoint, 60000);

        [stops addObject:[NSValue valueWithCMTime:time]];
        _currentPosition = initialTime;   // set the current position to the last frame displayed
        startPoint+=0.04; // the step between frames is 0.04sec
    }

    __block CMTime lastTime = CMTimeMake(-1, 1);
    __block int count = 0;
    [_imageGenerator generateCGImagesAsynchronouslyForTimes:stops
                                          completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime,AVAssetImageGeneratorResult result, NSError *error)
     {
         if (result == AVAssetImageGeneratorSucceeded)
         {
             if (CMTimeCompare(actualTime, lastTime) != 0)
             {
                 NSLog(@"new frame found");
                 lastTime = actualTime;
             }
             else
             {
                 NSLog(@"skipping");
                 return;
             }

             // place the image onto the view
             NSRect rect = CGRectMake((count+0.5) * 110, 500, 100, 100);

             NSImageView *iView = [[NSImageView alloc] initWithFrame:rect];

             [iView setImageScaling:NSScaleToFit];
             NSImage *myImage = [[NSImage alloc] initWithCGImage:image size:(NSSize){50.0,50.0}];
             [iView setImage:myImage];
             [self.windowForSheet.contentView addSubview: iView];
             [_viewsToRemove addObject:iView];
         }

         if (result == AVAssetImageGeneratorFailed)
         {
             NSLog(@"Failed with error: %@", [error localizedDescription]);
         }
         if (result == AVAssetImageGeneratorCancelled)
         {
             NSLog(@"Canceled");
         }
         count++;
     }];
}

}

If you have any thoughts or ideas, please share with me, I will really appreciate it!

Thank you

Eugene Gordin
  • 4,047
  • 3
  • 47
  • 80
  • Its a little bit late, but maybe it will help others. We use a UISlider and registered our method to the UIControlEventValueChanged of the slider. – Paul Schröder Apr 15 '15 at 10:23

1 Answers1

0

In order to make your NSSlider continuous, open your window controller's XIB file in Interface Builder and click on the NSSlider. Then, open the Utilities area

Utilities area

select the Attributes Inspector

Attributes Inspector

and check the "Continuous" checkbox

Continuous checked under Control header

under the Control header. Once you've done this, your IBAction sliderMove: will be called as the slider is moved rather than once the mouse is released.

Note: Alternatively, with an

NSSlider *slider = //...

one can simply call

[slider setContinuous:YES];
Nate Chandler
  • 4,533
  • 1
  • 23
  • 32