ReplayKit - 在ObjectiveC

时间:2016-10-06 12:41:15

标签: ios objective-c uiviewcontroller garbage-collection replaykit

我在使用ObjectiveC在ReplayKit中保持对RPPreviewViewController的引用时遇到问题,我想知道我做错了什么。

.h文件:

@interface ReplayKitHelper : NSObject <RPPreviewViewControllerDelegate, RPScreenRecorderDelegate>

-(void)startRecording;
-(void)stopRecording;
-(void)previewRecording;

@property(strong) RPPreviewViewController* previewViewControllerRef;

@end

.mm文件:

@implementation ReplayKitHelper

@synthesize previewViewControllerRef;

-(void)startRecording
{
    RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;

    recorder.delegate = self;

    [recorder startRecordingWithMicrophoneEnabled : true handler : ^ (NSError *error)
    {
    }];
}

-(void)stopRecording
{
    RPScreenRecorder* recorder = RPScreenRecorder.sharedRecorder;

    [recorder stopRecordingWithHandler : ^ (RPPreviewViewController * previewViewController, NSError * error)
    {
        if (error == nil)
        {
            if (previewViewController != nil)
            {
                previewViewControllerRef = previewViewController;
            }
        }
    }];
}

-(void)previewRecording
{
    if (previewViewControllerRef != nil)
    {
        previewViewControllerRef.modalPresentationStyle = UIModalPresentationFullScreen;
        previewViewControllerRef.previewControllerDelegate = self;
        [[IOSAppDelegate GetDelegate].IOSController presentViewController : previewViewControllerRef animated : YES completion : nil];
        // IOSController is my main UIViewController
    }
}
@end

在运行时期间,我按顺序启动方法startRecording,stopRecording和previewRecording。一切都很顺利,直到previewRecording,看起来它看起来像previewViewControllerRef不再有效(它不是零,但是当我试图引用它时崩溃)。

当我尝试在stopRecordingWithHandler块中运行[self previewRecording]时,在我传递引用之后 - 一切正常。

看起来应用程序离开块后,处理程序中的previewViewController就会被释放。

大多数示例都是用Swift编写的,不幸的是我被判为ObjectiveC。在Swift示例中,对previeViewController的引用只是传递给变量,但是在ObjectiveC中它似乎无效。

你有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我将假设你正在使用ARC,如果是这样的话,就不再需要合成属性了。

将接口文件中的RPPreviewViewController更改为:

@property(非原子,强)RPPreviewViewController * RPPreviewViewController;

删掉@synthesize。

然后在stopRecording处理程序中,您可以保留对可用RPPreviewViewController的引用,如下所示:

- (void)stopScreenRecording {
RPScreenRecorder *sharedRecorder = RPScreenRecorder.sharedRecorder;
[sharedRecorder stopRecordingWithHandler:^(RPPreviewViewController *previewViewController, NSError *error) {
    if (error) {
        NSLog(@"stopScreenRecording: %@", error.localizedDescription);
    }

    if (previewViewController) {
        self.previewViewController = previewViewController;
    }
}];
}

根据我的经验,ReplayKit仍然存在问题,而且还没有很多关于它的文档。

相关问题