在后台线程中运行方法后,应用崩溃 [英] App crashes after running a method in background thread

查看:78
本文介绍了在后台线程中运行方法后,应用崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新编码我们可以拖动图像的主屏幕.

I am recoding the main screen in which we can drag images.

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

I have a setneedsdisplay method which slows down the dragging of objects.

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

But when i call the same method in the background thread the issue is solved but the app crashes.

是否还有其他选择可以录制视频而无需一次又一次调用drawRect:方法?

Also if there is any other option to record the video without calling the drawRect: method again and again?

- (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进行操作,请在主线程中执行.

You are updating your UI from the background thread. Never do that,If you need to do something with your UI, do it in the main thread.

我也在您的代码上发现了一个问题:

Also I found an issue on your code:

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

You are calling the setNeedsDisplay from the drawRect: it'll call the drawRect: again.

(如果您需要更新视图调用[self setNeedsDisplay];,而从不直接调用drawRect:.)

(If you need to update your view call [self setNeedsDisplay]; and never call drawRect: directly.)

这篇关于在后台线程中运行方法后,应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆