Swift - Scanning with IKScannerDeviceView on OSX

筅森魡賤 提交于 2019-12-13 16:03:41

问题


I'm working on an OSX app that should be able to scan a view images, then generate a PDF and so on.

But I'm already stuck in the first step, since it seems I can't get IKScannerDeviceView to work and there is little to no help to be found.

This is what I have until now, following another question here:

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)
    }
}

This is what happens:

As you can see, I the scan button is gray and I can't scan. Scanning works when accessed in the settings

Edit: I tried to implement the ICDeviceBrowserDelegate like in the ScannerBrowser example from Apple, but no method is ever called, although the example from Apple works...

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)
    }
}

来源:https://stackoverflow.com/questions/38973881/swift-scanning-with-ikscannerdeviceview-on-osx

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