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