简体   繁体   English

在后台线程中运行方法后,应用崩溃

[英]App crashes after running a method in background thread

I am recoding the main screen in which we can drag images. 我正在重新编码我们可以在其中拖动图像的主屏幕。

I have a setneedsdisplay method which slows down the dragging of objects. 我有一个setneedsdisplay方法,它会减慢对象的拖动速度。

But when i call the same method in the background thread the issue is solved but the app crashes. 但是,当我在后台线程中调用相同的方法时,问题已解决,但应用程序崩溃了。

Also if there is any other option to record the video without calling the drawRect: method again and again? 另外,如果还有其他选项可以录制视频而无需一次又一次地调用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];

}

Possible cause: 可能的原因:

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

You are updating your UI from the background thread . 您正在从后台线程更新UI Never do that,If you need to do something with your UI, do it in the main thread. 永远不要这样做,如果您需要对UI进行操作,请在主线程中执行。

Also I found an issue on your code: 我也在您的代码上发现了一个问题:

You are calling the setNeedsDisplay from the drawRect: it'll call the drawRect: again. 您正在从drawRect:调用setNeedsDisplay drawRect:它将再次调用drawRect:

(If you need to update your view call [self setNeedsDisplay]; and never call drawRect: directly.) (如果需要更新视图,请调用[self setNeedsDisplay];不要直接调用drawRect:

I guess setNeedsDisplay: must be called on the main thread then. 我猜setNeedsDisplay:必须在主线程上调用。 Most UI methods don't work on background threads. 大多数UI方法不适用于后台线程。

If you put your render methods into your question we can see if they can be improved at all? 如果将渲染方法放入您的问题中,我们可以看看它们是否可以完全改善?

And try only setting setNeedsDisplayInRect: if you only want to redraw part of the display? 并尝试仅设置setNeedsDisplayInRect:如果只想重绘部分显示内容?

You should never call any method that updates the UI in the background thread. 您永远不应调用任何在后台线程中更新UI的方法。 Always update UI from the main thread. 始终从主线程更新UI。 You don't need that line there if talking about your code as drawrect is called when - setNeedsDisplay method is called. 如果在调用setNeedsDisplay方法时调用drawrect来谈论代码,则不需要在那一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iPhone应用程序从后台加载后崩溃 - iPhone app crashes after loading it from background 使用NSOperationQueue在后台线程和sizeWithFont中执行drawRect使应用崩溃 - performing drawRect in background thread and sizeWithFont using NSOperationQueue crashes app iOS:如果应用程序未在后台运行,则调用openURL会在启动时崩溃应用程序 - iOS : If app is not running in background,calling openURL crashes app on launch iPhone主线程崩溃,但应用程序仍在运行-“程序收到信号0” - Iphone main thread crashes but app still running - “program received signal 0” iOS App在线程0中崩溃 - iOS App crashes in Thread 0 在特定时间后终止在后台运行的应用程序? - Terminate the app running in background after a specific time? 如果应用程序在iPhone中进入后台模式,如何停止运行线程? - how to stop running thread if app went to background mode in iphone? 如何在我的 iphone 应用程序中取消后台运行线程 - how to cancel the background running thread in my iphone app 在后台运行代码后如何更新主线程上的数据? - How to update data on the main thread after running code in the background? 执行tableView的cellForRowAtIndexPath方法后,iPhone应用程序崩溃 - iPhone app crashes after execution of cellForRowAtIndexPath method of tableView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM