iOS掃描二維碼實(shí)現(xiàn)手勢拉近拉遠(yuǎn)鏡頭
在做掃碼需求,往往會(huì)有放大鏡頭需求。
蘋果提供了AVCaptureConnection中,videoScaleAndCropFactor:縮放裁剪系數(shù),使用該屬性,可以實(shí)現(xiàn)拉近拉遠(yuǎn)鏡頭。再結(jié)合手勢UIPinchGestureRecognizer,就很簡單實(shí)現(xiàn)手勢拉近拉遠(yuǎ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;
}
拉近拉遠(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ì)崩潰哦!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS中的多線程如何按設(shè)定順序去執(zhí)行任務(wù)詳解
多線程相信大家或多或少都有所了解吧,下面這篇文章主要給大家介紹了關(guān)于iOS中多線程如何按設(shè)定順序去執(zhí)行任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-12-12
iOS開發(fā)教程之APP內(nèi)部切換語言的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)教程之APP內(nèi)部切換語言的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
iOS中的音頻服務(wù)和音頻AVAudioPlayer音頻播放器使用指南
這里我們要介紹的是AVAudio ToolBox框架中的AudioServicesPlaySystemSound函數(shù)創(chuàng)建的服務(wù),特別適合用來制作鈴聲,下面就簡單整理一下iOS中的音頻服務(wù)和音頻AVAudioPlayer音頻播放器使用指南:2016-06-06
深入講解iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲(chǔ)方式
這篇文章主要介紹了iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲(chǔ)方式,包括plistXML屬性列表和NSKeydeArchiver歸檔兩個(gè)部分,需要的朋友可以參考下2015-12-12

