1

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
dmid
  • 483
  • 4
  • 18
  • When you say "no longer works" what do you mean? Does it timeout loading the interface or does it show what was there before but no inputs work? Did you try a stack trace right after the problem occurs? – Joshua Walcher Dec 11 '13 at 18:43
  • it the scanner view as it is when it first opens, but everything is greyed out and inactive. – dmid Dec 11 '13 at 19:19
  • is the device ever "closed"? – tofutim Dec 11 '13 at 21:59
  • Yes, the device is closed. `[mDeviceBrowser stop]` gets called in the `-(void)windowDidLoad` method. Now I can see that `[mDeviceBrowser start]` in the `-(id)init` method only gets called the first time the window is opened. – dmid Dec 12 '13 at 03:57

1 Answers1

1

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
dmid
  • 483
  • 4
  • 18