IKScannerDeviceView is only working once

旧时模样 提交于 2019-12-11 08:07:05

问题


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:


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


来源:https://stackoverflow.com/questions/20339108/ikscannerdeviceview-is-only-working-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!