对可可开发来说很新颖,并且确实存在一个根本性问题。图像生成器(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++;
}];
}
}
如果你有任何想法或意见,请与我分享,我真的很感激!
谢谢
它来得有点晚,但也许这将帮助别人。 我们使用UISlider并将我们的方法注册到滑块的UIControlEventValueChanged。 – user1128713