1

我有一个带有主窗口的应用程序和一个带有 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
4

1 回答 1

1

我已经解决了这个问题(在与 Apple 交谈之后),结果是这样的:

NSScannerDeviceView 在窗口初始化时被激活,但是由于它不会再次被调用并且 NSScannerDeviceView 在窗口关闭时关闭,它永远不会被重新激活。

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

#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
于 2014-01-03T09:14:40.097 回答