iphone - 在后台线程中运行方法后应用程序崩溃

标签 iphone ios objective-c xcode ios5

我正在重新编写我们可以在其中拖动图像的主屏幕。

我有一个 setneedsdisplay 方法可以减慢对象的拖动速度。

但是当我在后台线程中调用相同的方法时,问题已解决,但应用程序崩溃了。

此外,是否有任何其他选项可以在不一次又一次调用 drawRect: 方法的情况下录制视频?

- (void) drawRect:(CGRect)rect {
    NSDate* start = [NSDate date];
    CGContextRef context = [self createBitmapContextOfSize:self.frame.size];


    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, self.frame.size.height);
    CGContextConcatCTM(context, flipVertical);

    [self.layer renderInContext:context];

    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    UIImage* background = [UIImage imageWithCGImage: cgImage];
    CGImageRelease(cgImage);

    self.currentScreen = background;

    if (_recording) {
        float millisElapsed = [[NSDate date] timeIntervalSinceDate:startedAt] * 1000.0;
        [self writeVideoFrameAtTime:CMTimeMake((int)millisElapsed, 1000)];

    }

    float processingSeconds = [[NSDate date] timeIntervalSinceDate:start];
     delayRemaining = (1.0 / self.frameRate) - processingSeconds;



    //CGContextRelease(context);

    //redraw at the specified framerate
    //[self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:delayRemaining > 0.0 ? delayRemaining : 0.01];

    [self performSelectorInBackground:@selector(setNeedsDisplay) withObject:nil];

}

最佳答案

可能的原因:

[self performSelectorInBackground:@selector(setNeedsDisplay) withObject:nil];

您正在从后台线程更新您的用户界面。 永远不要那样做,如果您需要对 UI 做一些事情,请在主线程中进行。

我还发现您的代码存在问题:

您正在从 drawRect: 调用 setNeedsDisplay,它将再次调用 drawRect:

(如果您需要更新 View ,请调用 [self setNeedsDisplay]; 并且永远不要直接调用 drawRect:。)

关于iphone - 在后台线程中运行方法后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14771982/

相关文章:

ios - 在 Storyboard 中将 Segue 拖到自身

ios - 如何设置视频大小,使其适用于所有没有黑边的 iOS 设备

ios - 异构集合文字只能推断为 '[String : Any]' ;如果这是有意的,请添加显式类型注释

objective-c - Xcode 4 中的 OCUnit 和 C++?

iphone - 计算 Objective-C 中的网络时间协议(protocol)(偏移量)

ios - UIview 在调用 View Controller 时没有响应

iphone - UITextView 中简单链接的最佳方法

iphone - 使用跨度缩放到用户位置

ios - URLSession 成功,但返回空 JSON 数据编辑 JSON 解析问题

objective-c - 在与主类分开的 .m 文件中实现 objc 协议(protocol)方法?