objective-c - iPad GUI 仅在触摸后更新

标签 objective-c ios multithreading ipad

我正在为 iPad 构建一个多线程应用程序。我有一个从影片剪辑中提取帧的类,我有一个 UIScrollView 来显示我提取的内容。帧提取器在不同的线程中运行,我只想在提取一定数量的帧后才开始构建 ScrollView 。因此,我创建了这个名为 buffering 的 BOOL 属性,它通过线程进行更新。我的 View Controller 观察到这个属性,只有在这个属性等于 NO 之后我才开始构建 ScrollView。

问题是调用构建方法后,我在 GUI 中看不到任何变化。触摸屏幕后只能看到 ScrollView

这是我在做什么:

创建线程:

[NSThread detachNewThreadSelector:@selector(startReading) toTarget:self withObject:nil];

线程中运行的代码:

-(void) startReadingWithTimeRange:(STimeRange *) timeRange;
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool
    //setting the time range for reading the file
    [assetReader setTimeRange:timeRange.timeRange];

    //start reading
    [assetReader startReading];

    //declearing about the buffer
    CMSampleBufferRef buffer;
    int z = 0;
    while ( [assetReader status]==AVAssetReaderStatusReading)
    {
        z++;
        //reading buffer
        buffer = [assetReaderTrackOutput copyNextSampleBuffer];

        if (buffer == NULL) break;
        //converting the buffer to UIImage
        UIImage *frameImage = imageFromSampleBuffer(buffer);
        CMTime time = CMSampleBufferGetOutputPresentationTimeStamp(buffer);
        NSLog(@"###duration %lld",time.value/time.timescale);


        SFrame *frame = [[SFrame alloc] initWithImage:frameImage andTime:time];
        //add it to the frame array
        [framesArray addObject:frame];

        [frame release];
        //check if buffer is not null and needed release


        //release the buffer
        CFRelease(buffer);


        if ([framesArray count] > 100 && self.buffering) {
            [self willChangeValueForKey:@"buffering"];
            self.buffering = NO;
            [self didChangeValueForKey:@"buffering"];

        }


    }
    [pool release];

}

我真的一无所知,非常感谢任何帮助

最佳答案

首先不要调用 willChangeValueForKey 和 didChangeValueForKey 因为你只是让 KVO 通知触发两次,它已经被 self.buffering = NO 触发了;

其次,主线程之外的属性/ivar 更改应该在@synchronized block 内完成,例如:

@synchronized (self) {
     self.buffering = NO;
}

您可以交替使用 performSelectorOnMainThread 来调用缓冲方法,例如:

[self performSelectorOnMainThread:@selector(buffering:) withObject:[NSNumber numberWithBool:YES] waitUntilDone:NO];

如果这样做不能解决您的问题,那么您的代码中可能存在其他问题,例如需要在某些 View 或种类上调用 setNeedsDisplay。

你没有说 KVO 通知会发生什么,你不明白这就是你明确尝试发送它的原因吗?

关于objective-c - iPad GUI 仅在触摸后更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8043789/

相关文章:

iphone - 检查 iPhone 中的 cookie 是否过期

ios - Xcode:如何将 MP4 文件转换为音频文件

android postDelayed 和 removeCallbacksAndMessages 同步

c# - Windows 消息泵中的清理消息

iphone - 在 iOS 的 json 中使用未声明的标识符

ios - UITextView 链接点击识别延迟

objective-c - append 到 NSTextView

ios - UITextField.enabled 为 Yes 或 No 根据 UISwitch.on

multithreading - 这个字数 bolt 线程如何安全?

ios - 调用 [NSURLSessionDataDelegate resume] 时的 SIGSEGV