以前記錄了用Apple原裝的AVFoundationo類庫掃描二維碼:http://www.cnblogs.com/foxting/p/4733226.html,此篇記錄下如何設置掃描區域。html
AVCaptureMetadataOutput有一個屬性rectOfInterest,設置你的區域便可。swift
先把Video區域設置成全屏的:self.layer?.frame = self.view.boundside
看下rectOfInterest的註釋會知道它的區域大小是先設置top再設置left,有效範圍爲(0,0,1,1)this
@discussion
The value of this property is a CGRect that determines the receiver's rectangle of interest for each frame of video.
The rectangle's origin is top left and is relative to the coordinate space of the device providing the metadata. Specifying
a rectOfInterest may improve detection performance for certain types of metadata. The default value of this property is the
value CGRectMake(0, 0, 1, 1). Metadata objects whose bounds do not intersect with the rectOfInterest will not be returned.spa
鍵入如下代碼rest
let x = (self.view.bounds.size.width - 268) / 2 let beginningX = x / self.view.bounds.width let beginningY = 106 / self.view.bounds.height let width = 268 / self.view.bounds.width let height = 268 / self.view.bounds.height let interestedRect = CGRectMake(beginningY, beginningX, height, width) let output = AVCaptureMetadataOutput() output.rectOfInterest = interestedRect
實現的效果仍是能夠的,此設置仍是蠻方便的。上圖咯:orm