From b08dd384d78a9c3e1c7dbc2a2b12e97d19fac9a9 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 May 2017 13:23:19 +0200 Subject: [PATCH] Swift 3 update --- QRIO.xcodeproj/project.pbxproj | 12 +++++------ QRIO/QRIO.swift | 38 +++++++++++++++++----------------- QRIOTests/QRIOTests.swift | 2 +- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/QRIO.xcodeproj/project.pbxproj b/QRIO.xcodeproj/project.pbxproj index c3a0d70..5688f1e 100644 --- a/QRIO.xcodeproj/project.pbxproj +++ b/QRIO.xcodeproj/project.pbxproj @@ -151,11 +151,11 @@ TargetAttributes = { 838FB5641D26A45E0065A950 = { CreatedOnToolsVersion = 7.3.1; - LastSwiftMigration = 0800; + LastSwiftMigration = 0830; }; 838FB56E1D26A45E0065A950 = { CreatedOnToolsVersion = 7.3.1; - LastSwiftMigration = 0800; + LastSwiftMigration = 0830; }; }; }; @@ -329,7 +329,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -348,7 +348,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.nodes.QRIO; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -359,7 +359,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.nodes.QRIOTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -370,7 +370,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.nodes.QRIOTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/QRIO/QRIO.swift b/QRIO/QRIO.swift index 617252f..560f515 100644 --- a/QRIO/QRIO.swift +++ b/QRIO/QRIO.swift @@ -10,16 +10,16 @@ import UIKit import AVFoundation import CoreImage -public class QRInput: NSObject, AVCaptureMetadataOutputObjectsDelegate { - private var session: AVCaptureSession? - private var previewLayer: AVCaptureVideoPreviewLayer? +open class QRInput: NSObject, AVCaptureMetadataOutputObjectsDelegate { + fileprivate var session: AVCaptureSession? + fileprivate var previewLayer: AVCaptureVideoPreviewLayer? - public var imageScanCompletionBlock: ((string: String) -> ())? + open var imageScanCompletionBlock: ((_ string: String) -> ())? - public func scanForQRImage(previewIn previewContainer: UIView? = nil, rectOfInterest: CGRect? = nil, completion: ((string: String) -> ())) { + open func scanForQRImage(previewIn previewContainer: UIView? = nil, rectOfInterest: CGRect? = nil, completion: @escaping ((_ string: String) -> ())) { session = AVCaptureSession() - let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) + let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) do { let input = try AVCaptureDeviceInput(device: device) @@ -30,7 +30,7 @@ public class QRInput: NSObject, AVCaptureMetadataOutputObjectsDelegate { } let output = AVCaptureMetadataOutput() - output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) + output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main) session?.addOutput(output) output.metadataObjectTypes = [AVMetadataObjectTypeQRCode] @@ -43,16 +43,16 @@ public class QRInput: NSObject, AVCaptureMetadataOutputObjectsDelegate { previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill } - dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [weak self] in + DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async { [weak self] in self?.session?.startRunning() } - if let rectOfInterest = rectOfInterest, previewLayer = previewLayer { - output.rectOfInterest = previewLayer.metadataOutputRectOfInterestForRect(rectOfInterest) + if let rectOfInterest = rectOfInterest, let previewLayer = previewLayer { + output.rectOfInterest = previewLayer.metadataOutputRectOfInterest(for: rectOfInterest) } } - public func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { + open func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { var QRCode: String? for metadata in metadataObjects as! [AVMetadataObject] { if metadata.type == AVMetadataObjectTypeQRCode { @@ -60,11 +60,11 @@ public class QRInput: NSObject, AVCaptureMetadataOutputObjectsDelegate { } } if let code = QRCode { - imageScanCompletionBlock?(string: code) + imageScanCompletionBlock?(code) } } - public func finish() { + open func finish() { imageScanCompletionBlock = nil if let session = session { session.stopRunning() @@ -82,9 +82,9 @@ public class QRInput: NSObject, AVCaptureMetadataOutputObjectsDelegate { public extension UIImage { - public static func QRImageFromString(string: String, containingViewSize: CGSize? = nil, correctionLevel: String = "L") -> UIImage? { - let stringData = string.dataUsingEncoding(NSISOLatin1StringEncoding) - let filter = CIFilter(name: "CIQRCodeGenerator") + public static func QRImageFrom(string: String, containingViewSize: CGSize? = nil, correctionLevel: String = "L") -> UIImage? { + let stringData = string.data(using: String.Encoding.isoLatin1) + let filter = CIFilter(name: "CICode128BarcodeGenerator") filter?.setValue(stringData, forKey: "inputMessage") filter?.setValue(correctionLevel, forKey: "inputCorrectionLevel") @@ -97,10 +97,10 @@ public extension UIImage { scaleY = size.height / resultImage.extent.size.height } - let qrImage = resultImage.imageByApplyingTransform(CGAffineTransformMakeScale(scaleX, scaleY)) + let qrImage = resultImage.applying(CGAffineTransform(scaleX: scaleX, y: scaleY)) let context = CIContext() - if let tempImage: CGImageRef = context.createCGImage(qrImage, fromRect: qrImage.extent) { - return UIImage(CGImage: tempImage) + if let tempImage: CGImage = context.createCGImage(qrImage, from: qrImage.extent) { + return UIImage(cgImage: tempImage) } return nil } diff --git a/QRIOTests/QRIOTests.swift b/QRIOTests/QRIOTests.swift index 5f6fe39..fa9208a 100644 --- a/QRIOTests/QRIOTests.swift +++ b/QRIOTests/QRIOTests.swift @@ -28,7 +28,7 @@ class QRIOTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock { + self.measure { // Put the code you want to measure the time of here. } }