bharath
bharath

Reputation: 953

Perform action as soon as camera opens ios

I am using ZBAR scanner for scanning bar codes. But it takes quite some time before the camera opens. In the meantime, i want to have a loading animation once the button is pressed and stop the animation after the camera is open. Can anyone plz tell how to detect as soon as the camera is open event handler in ios.

Upvotes: 0

Views: 68

Answers (1)

yonasstephen
yonasstephen

Reputation: 2725

I'm not sure if this is what you are looking for, but I think you can use KVO (Key-value observing) to have event-handler mechanism.

There is this connected property that is KVO-compliant that might tell you when the camera opens (I am not sure though). Here's the documentation:

connected

Indicates whether the device is currently connected. (read-only)

@property(nonatomic, readonly, getter=isConnected) BOOL connected

Discussion The value of this property indicates whether the device represented by the receiver is connected and available for use as a capture device. When the value of this property becomes NO for a given instance, however, it will not become YES again. If the same physical device again becomes available to the system, it will be represented using a new instance of AVCaptureDevice.

You can observe the value of this property using Key-value observing to be notified when a device is no longer available.

Then you can try to implement observer for this connected property by following the link below. Basically what you need to do is something like:

Event Handler

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if( [keyPath isEqualToString:@"connected"] ){
        BOOL isConnected = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
        if(isConnected){
            //remove loading icon..
        } else {
            //show loading icon..
        }
    }
}

Event Registration

- (void)viewWillAppear:(BOOL)animated{
    AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    int flags = NSKeyValueObservingOptionNew; 
    [camDevice addObserver:self forKeyPath:@"connected" options:flags context:nil];

    (...)   
}

Here's the documentation that might help you:

  1. KVO Programming Guide
  2. NSKeyValueObserving Protocol

Hope this helps you! :)

Upvotes: 1

Related Questions