iOS掃描二維碼實現(xiàn)手勢拉近拉遠鏡頭
在做掃碼需求,往往會有放大鏡頭需求。
蘋果提供了AVCaptureConnection中,videoScaleAndCropFactor:縮放裁剪系數(shù),使用該屬性,可以實現(xiàn)拉近拉遠鏡頭。再結(jié)合手勢UIPinchGestureRecognizer,就很簡單實現(xiàn)手勢拉近拉遠鏡頭。
手勢代碼
///記錄開始的縮放比例
@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;
}
拉近拉遠鏡頭代碼
- (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];
}
有一點需要注意:the videoScaleAndCropFactor property may be set to a value in the range of 1.0 to videoMaxScaleAndCropFactor,videoScaleAndCropFactor這個屬性取值范圍是1.0-videoMaxScaleAndCropFactor,如果你設(shè)置超出范圍會崩潰哦!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
為textView添加語音輸入功能的實例代碼(集成訊飛語音識別)
下面小編就為大家分享一篇為textView添加語音輸入功能的實例代碼(集成訊飛語音識別),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
iOS 隱藏導(dǎo)航條和狀態(tài)欄實現(xiàn)方法
這篇文章主要介紹了 iOS隱藏導(dǎo)航條和狀態(tài)欄實現(xiàn)方法的相關(guān)資料,有時候根據(jù)需求開發(fā)APP 需要隱藏導(dǎo)航欄和狀態(tài)欄,這里提供了實現(xiàn)方法需要的朋友可以參考下2016-11-11
淺談Unity中IOS Build Settings選項的作用
下面小編就為大家分享一篇淺談Unity中IOS Build Settings選項的作用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
基于ios逆向過程中l(wèi)ldb調(diào)試技巧(推薦)
下面小編就為大家?guī)硪黄趇os逆向過程中l(wèi)ldb調(diào)試技巧(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07

