IKScannerDeviceView is only working once

677 views Asked by At

I have an app with a main window and a secondary window with an NSScannerDeviceView in it. The idea is to scan an image to memory and place it into an NSImageView in the main window.

So far so good. When the scanner window is opened I can scan an image and it is placed correctly in to the NSImageView.

The problem is this, I can scan again and again but if I close the scan window and re-open it the NSScannerDeviceView no longer works. I'm sure the solution is relatively simple but as I'm still learning obj-c / cocoa it's got me stumped.

The code I'm using is as follows: ScanWindowController.h

#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
#import <ImageCaptureCore/ImageCaptureCore.h>

@interface ScanWindowController : NSWindowController <IKScannerDeviceViewDelegate, ICScannerDeviceDelegate, ICDeviceBrowserDelegate> {

ICDeviceBrowser *mDeviceBrowser;

}

@property (strong) IBOutlet IKScannerDeviceView *ScannerView;

@end

and: ScannerWindowController.m

#import "ScanWindowController.h"
#import "AppDelegate.h"

@interface ScanWindowController ()

@end

@implementation ScanWindowController

@synthesize ScannerView;

- (id)init
{
    if ( ! (self = [super initWithWindowNibName: @"ScanWindowController"]) ) {
        return nil;
    }

mDeviceBrowser = [[ICDeviceBrowser alloc] init];
mDeviceBrowser.delegate = self;
mDeviceBrowser.browsedDeviceTypeMask = ICDeviceLocationTypeMaskLocal|ICDeviceLocationTypeMaskRemote|ICDeviceTypeMaskScanner;
[mDeviceBrowser start];

return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];
}


- (void)windowWillClose:(NSNotification *)notification {
    [mDeviceBrowser stop];
}


- (void)scannerDeviceView:(IKScannerDeviceView *)scannerDeviceView didScanToURL:(NSURL *)url fileData:(NSData *)data error:(NSError *)error
{

    if(!error) {

        [(AppDelegate*)[[NSApplication sharedApplication] delegate] recieveScannedData:data];

    }

}


- (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
{

[scanner requestOpenSession];
}


- (void)deviceBrowser:(ICDeviceBrowser*)browser didAddDevice:(ICDevice*)addedDevice moreComing:(BOOL)moreComing
{


    if ( (addedDevice.type & ICDeviceTypeMaskScanner) == ICDeviceTypeScanner )
    {

        [ScannerView setScannerDevice:(ICScannerDevice*)addedDevice];

}
}


-(void)deviceBrowser:(ICDeviceBrowser *)browser didRemoveDevice:(ICDevice *)device moreGoing:(BOOL)moreGoing
{
[device requestCloseSession];
}


-(void)didRemoveDevice:(ICDevice*)removedDevice
{
    [removedDevice requestCloseSession];
}


@end
1

There are 1 answers

0
dmid On BEST ANSWER

I have solved this problem (after talking to Apple), and the result is this:

The NSScannerDeviceView is activated when the window is initialised, however as this doesn't get called again and the NSScannerDeviceView is closed when the window is closed, it never gets reactivated.

The solution was to create a new method (scannerReopen) and call it each time the window is opened as follows:

#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
#import <ImageCaptureCore/ImageCaptureCore.h>

@interface ScanWindowController : NSWindowController <IKScannerDeviceViewDelegate, ICScannerDeviceDelegate, ICDeviceBrowserDelegate> {

ICDeviceBrowser *mDeviceBrowser;

}

@property (strong) IBOutlet IKScannerDeviceView *ScannerView;

-(void)scannerReopen;

@end

and:

#import "ScanWindowController.h"
#import "AppDelegate.h"

@interface ScanWindowController ()

@end

@implementation ScanWindowController

@synthesize ScannerView;


- (id)init
{
    if ( ! (self = [super initWithWindowNibName: @"ScanWindowController"]) ) {
    return nil;
}

if(!mDeviceBrowser) {
    mDeviceBrowser = [[ICDeviceBrowser alloc] init];
    [mDeviceBrowser setDelegate:self];
    mDeviceBrowser.browsedDeviceTypeMask = ICDeviceLocationTypeMaskLocal|ICDeviceLocationTypeMaskRemote|ICDeviceTypeMaskScanner;
    [mDeviceBrowser start];
}

return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];
}


- (void)windowWillClose:(NSNotification *)notification {
[mDeviceBrowser stop];
}


- (void)scannerDeviceView:(IKScannerDeviceView *)scannerDeviceView didScanToURL:(NSURL *)url fileData:(NSData *)data error:(NSError *)error
{
//process the scanned data...
if(!error) {

    [(AppDelegate*)[[NSApplication sharedApplication] delegate] receiveScannedData:data];

}

}


- (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
{

[scanner requestOpenSession];
}


- (void)deviceBrowser:(ICDeviceBrowser*)browser didAddDevice:(ICDevice*)addedDevice moreComing:(BOOL)moreComing
{


if ( (addedDevice.type & ICDeviceTypeMaskScanner) == ICDeviceTypeScanner )
{

    [ScannerView setScannerDevice:(ICScannerDevice*)addedDevice];

}
}


-(void)deviceBrowser:(ICDeviceBrowser *)browser didRemoveDevice:(ICDevice *)device moreGoing:(BOOL)moreGoing
{
[device requestCloseSession];
}


-(void)didRemoveDevice:(ICDevice*)removedDevice
{
    [removedDevice requestCloseSession];
}


-(void)scannerReopen
{

[mDeviceBrowser start];

}

@end