我正在使用AVCaptureSession在Swift应用程序中扫描QR码。我想在检测到的QR码周围画一个方框,但是在将AVMetadataMachineReadableCodeObject的“ corners”属性转换为可用的对象时遇到了麻烦。
var corners:[AnyObject]! {得到}
此属性的值是CFDictionary对象的数组,每个对象
其中一个是使用CGPoint结构体通过
CGPointCreateDictionaryRepresentation函数,表示
对象的角相对于图像的坐标
它所在的位置。
我已经尝试过此操作(基于project by werner77),但是出现以下编译器错误“'CGPoint?”与“ CGPoint”不同
// MARK: - AVCaptureMetadataOutputObjectsDelegate
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
let metadataObject = metadataObjects[0] as AVMetadataMachineReadableCodeObject;
var corners = metadataObject.corners as Array<NSDictionary>;
var topLeftDict = corners[0] as NSDictionary;
var topLeft : CGPoint?
// COMPILE ERROR: 'CGPoint?' is not identical to 'CGPoint'
CGPointMakeWithDictionaryRepresentation(topLeftDict, &topLeft)
}
任何帮助,将不胜感激。
最佳答案
了解可选内容。
任何类型都可以附加?
,这意味着它也可以是nil
。这样做的好处是,您必须显式地寻址或忽略nil
对象,这与Objective-C中的nil
对象可能导致无法追踪的错误不同。
从字典中获取对象时,它必须是可选的,因为字典中可能没有密钥。 CGPointMakeWithDictionaryRepresentation
期望为非可选,因此您必须使用初始化的非可选。
// Playground:
var point = CGPointMake(1, 2)
var dictionary = CGPointCreateDictionaryRepresentation(point)
var aPoint = CGPointZero
CGPointMakeWithDictionaryRepresentation(dictionary, &aPoint)
aPoint // x 1, y 2
关于ios - Swift中的CGPointMakeWithDictionaryRepresentation,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26124371/