我正在用Swift构建一个二维码扫描器,一切都在这方面有效.我遇到的问题是我试图使整个可见的一小部分区域AVCaptureVideoPreviewLayer能够扫描QR码.我发现,为了指定屏幕的哪个区域能够读取/捕获QR码,我必须使用AVCaptureMetadataOutput被调用的属性rectOfInterest.麻烦的是,当我将其分配给CGRect时,我无法扫描任何内容.在网上做了更多的研究后,我发现有些人建议我需要使用一个方法来调用metadataOutputRectOfInterestForRectCGRect,将其转换为属性rectOfInterest实际可以使用的正确格式.但是,metadataoutputRectOfInterestForRect我现在遇到的一个大问题是,当我使用这种方法时,我收到一个错误CGAffineTransformInvert: singular matrix.谁能告诉我为什么我会收到这个错误?我相信我根据Apple开发人员文档正确使用这种方法,我相信我需要根据我在网上找到的所有信息来实现我的目标.我将包括到目前为止我找到的文档的链接以及我用来扫描QR码的函数的代码示例
代码示例
func startScan() {
// Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
// as the media type parameter.
let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
// Get an instance of the AVCaptureDeviceInput class using the previous device object.
var error:NSError?
let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error)
if (error != nil) {
// If any error occurs, …Run Code Online (Sandbox Code Playgroud)