溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS掃描二維碼如何實(shí)現(xiàn)手勢(shì)拉近拉遠(yuǎn)鏡頭

發(fā)布時(shí)間:2021-08-06 14:03:15 來源:億速云 閱讀:162 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)iOS掃描二維碼如何實(shí)現(xiàn)手勢(shì)拉近拉遠(yuǎn)鏡頭,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

在做掃碼需求,往往會(huì)有放大鏡頭需求。

蘋果提供了AVCaptureConnection中,videoScaleAndCropFactor:縮放裁剪系數(shù),使用該屬性,可以實(shí)現(xiàn)拉近拉遠(yuǎn)鏡頭。再結(jié)合手勢(shì)UIPinchGestureRecognizer,就很簡(jiǎn)單實(shí)現(xiàn)手勢(shì)拉近拉遠(yuǎn)鏡頭。

手勢(shì)代碼

///記錄開始的縮放比例
@property(nonatomic,assign)CGFloat beginGestureScale;

///最后的縮放比例
@property(nonatomic,assign)CGFloat effectiveScale;

- (void)cameraInitOver
{
 if (self.isVideoZoom) {
  UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];
  pinch.delegate = self;
  [self.view addGestureRecognizer:pinch];
 }
}

- (void)pinchDetected:(UIPinchGestureRecognizer*)recogniser
{
 self.effectiveScale = self.beginGestureScale * recogniser.scale;
 if (self.effectiveScale < 1.0){
  self.effectiveScale = 1.0;
 }
 [self.scanObj setVideoScale:self.effectiveScale];

}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
 if ( [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] ) {
  _beginGestureScale = _effectiveScale;
 }
 return YES;
}

拉近拉遠(yuǎn)鏡頭代碼

- (void)setVideoScale:(CGFloat)scale
{
 [_input.device lockForConfiguration:nil];

 AVCaptureConnection *videoConnection = [self connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]];
 CGFloat maxScaleAndCropFactor = ([[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] videoMaxScaleAndCropFactor])/16;

 if (scale > maxScaleAndCropFactor)
  scale = maxScaleAndCropFactor;

 CGFloat zoom = scale / videoConnection.videoScaleAndCropFactor;

 videoConnection.videoScaleAndCropFactor = scale;

 [_input.device unlockForConfiguration];

 CGAffineTransform transform = _videoPreView.transform;
 [CATransaction begin];
 [CATransaction setAnimationDuration:.025];

  _videoPreView.transform = CGAffineTransformScale(transform, zoom, zoom);

 [CATransaction commit];

}

有一點(diǎn)需要注意:the videoScaleAndCropFactor property may be set to a value in the range of 1.0 to videoMaxScaleAndCropFactor,videoScaleAndCropFactor這個(gè)屬性取值范圍是1.0-videoMaxScaleAndCropFactor,如果你設(shè)置超出范圍會(huì)崩潰哦!

關(guān)于“iOS掃描二維碼如何實(shí)現(xiàn)手勢(shì)拉近拉遠(yuǎn)鏡頭”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI