2012-11-06 21 views
0

對可可開發來說很新穎,並且確實存在一個根本性問題。圖像生成器(generateCGImagesAsynchronouslyForTimes方法)不會提供實時結果

因此,簡而言之,我的應用UI看起來像一個簡單的窗口,底部有一個nsslider。我需要的是生成N個圖像並將它們放到我的應用程序窗口的N個nsviews中。

它能做什麼至今:

  • 我點擊滑塊(持有它),並拖動它。當我拖動它時,我的視圖中沒有任何反應(圖片不會生成)。當我釋放滑塊時,圖片已經生成,我的視圖中充滿了它們。

我想要什麼: - 我需要的意見,因爲我移動滑塊來填充圖片。

我想出了NSSlider屬性上的小複選框,它是連續的,我正在使用它,但是我的圖像生成器在我釋放滑塊之前仍然不會執行任何操作。

這裏是我的代碼:

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

}

如果你有任何想法或意見,請與我分享,我真的很感激!

謝謝

+0

它來得有點晚,但也許這將幫助別人。 我們使用UISlider並將我們的方法註冊到滑塊的UIControlEventValueChanged。 – user1128713

回答

0

爲了使您的NSSlider連續,在Interface Builder中打開你的窗控制器的XIB文件並點擊NSSlider。然後,打開公用事業領域

Utilities area

選擇屬性檢查器

Attributes Inspector

,並檢查 「連續」 複選框

Continuous checked under Control header

控制標題下。完成此操作後,將在滑塊移動而不是釋放鼠標後調用IBActionsliderMove:

注:此外,與

NSSlider *slider = //... 

一個可以簡單地調用

[slider setContinuous:YES]; 
+0

我非常感謝你的長期解釋,但我認爲你沒有仔細閱讀我的問題...... –

相關問題