IKScannerDeviceView只工作一次

时间:2013-12-02 22:22:25

标签: cocoa nswindowcontroller ikscannerdeviceview

我有一个带有主窗口的应用程序和一个带有NSScannerDeviceView的辅助窗口。 我们的想法是将图像扫描到内存并将其放入主窗口中的NSImageView中。

到目前为止一切顺利。当扫描仪窗口打开时,我可以扫描图像并将其正确放入NSImageView。

问题在于,我可以一次又一次扫描,但如果我关闭扫描窗口并重新打开它,则NSScannerDeviceView不再有效。我确信解决方案相对简单但是我还在学习obj-c / cocoa,这让我很难过。

我正在使用的代码如下: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

和: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 个答案:

答案 0 :(得分:1)

我已经解决了这个问题(与Apple交谈后),结果如下:

初始化窗口时会激活NSScannerDeviceView,但是当窗口关闭时,不会再次调用NSScannerDeviceView,它永远不会重新激活。

解决方案是创建一个新方法(scannerRendn)并在每次打开窗口时调用它,如下所示:

#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

#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