Search code examples
iosinheritanceavcapturedevice

Is it possible to create a class inherited from AVCaptureDevice?


I am trying to create a class inherited from AVCaptureDevice like this

in MyClass.h file

@interface MyClass : AVCaptureDevice

but how can I assign the AVCaptureDevice instance to "self" in init method?

Here is my approach now (doesn't work...)

in MyClass.m file

- (id) init
{
    self = [super init];
    if(self)
    {
       NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
       AVCaptureDevice *captureDevice = [devices firstObject];
       self = (MyClass*)captureDevice;
       [self myClassMethod];
    }
    return self;
}

- (void) myClassMethod
{
    NSLog(@"Doesn't work");
}

I got NSInvalidArgumentException, reason: '-[AVCaptureFigVideoDevice test]: unrecognized selector sent to instance", which make sense because test is not the method in AVCaptureDevice.

Another approach I did before is containing AVCaptureDevice in MyClass class, which works.

But I still wonder whether I miss some good approach to implement this thought.

Thank you.


Solution

  • AVCaptureDevice is not intended for subclassing.
    There is no way to create a concrete instance of this class without using one of the class methods:

    + devices
    + deviceWithUniqueID:
    + defaultDeviceWithMediaType:
    + devicesWithMediaType:
    

    Your options are either contain the instance and wrap access to it, create a proxy or some wrapper for it, or even a category.