我正在开发一个 OSX 应用程序,它应该能够扫描视图图像,然后生成 PDF 等等。
但是我已经陷入了第一步,因为我似乎无法让 IKScannerDeviceView 工作,而且几乎找不到任何帮助。
这是我到目前为止所拥有的,这里有另一个问题:
import Cocoa
import Quartz
class ViewController: NSViewController, IKScannerDeviceViewDelegate, ICScannerDeviceDelegate, ICDeviceBrowserDelegate {
@IBOutlet var scannerView: IKScannerDeviceView!
var deviceBrowser:ICDeviceBrowser!
override func viewDidLoad() {
super.viewDidLoad()
scannerView.delegate = self
deviceBrowser = ICDeviceBrowser()
deviceBrowser.delegate = self
deviceBrowser.browsedDeviceTypeMask = .Scanner
deviceBrowser.start()
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
func scannerDeviceDidBecomeAvailable(scanner: ICScannerDevice) {
scanner.requestOpenSession()
}
func deviceBrowser(browser: ICDeviceBrowser, didAddDevice device: ICDevice, moreComing: Bool) {
if device.type == ICDeviceType.Scanner{
scannerView.scannerDevice = device as! ICScannerDevice
}
}
func deviceBrowser(browser: ICDeviceBrowser, didRemoveDevice device: ICDevice, moreGoing: Bool) {
device.requestCloseSession()
}
func didRemoveDevice(device: ICDevice) {
device.requestCloseSession()
}
func device(device: ICDevice, didEncounterError error: NSError?) {
print("error")
print(error?.description)
}
}
如您所见,我的扫描按钮是灰色的,我无法扫描。在设置中访问时扫描有效
编辑:我尝试像 Apple 的 ScannerBrowser 示例中那样实现 ICDeviceBrowserDelegate ,但没有调用任何方法,尽管 Apple 的示例有效......
import Cocoa
import Quartz
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var deviceBrowser:ICDeviceBrowser!
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
deviceBrowser = ICDeviceBrowser()
deviceBrowser.delegate = self
deviceBrowser.browsedDeviceTypeMask = ICDeviceTypeMask(rawValue:
ICDeviceLocationTypeMask.Local.rawValue |
ICDeviceLocationTypeMask.Shared.rawValue |
ICDeviceLocationTypeMask.Bonjour.rawValue |
ICDeviceLocationTypeMask.Remote.rawValue |
ICDeviceLocationTypeMask.Bluetooth.rawValue)!
deviceBrowser.start()
print(deviceBrowser)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
extension AppDelegate: ICDeviceBrowserDelegate{
func deviceBrowser(browser: ICDeviceBrowser, didAddDevice device: ICDevice, moreComing: Bool) {
print("didAddDevice")
print(device)
}
func deviceBrowser(browser: ICDeviceBrowser, didRemoveDevice device: ICDevice, moreGoing: Bool) {
print("didRemoveDevice")
print(device)
}
func deviceBrowser(browser: ICDeviceBrowser, deviceDidChangeName device: ICDevice) {
print("deviceDidChangeName")
print(device)
}
func deviceBrowser(browser: ICDeviceBrowser, deviceDidChangeSharingState device: ICDevice) {
print("deviceDidChangeSharingState")
print(device)
}
func deviceBrowser(browser: ICDeviceBrowser, requestsSelectDevice device: ICDevice) {
print("requestSelectDevice")
print(device)
}
}