AVCaptureVideoPreviewLayer 相機(jī)截屏

很多文章都提到是普通view或者layer的截屏方法,就不再重復(fù)

針對(duì)特殊的?AVCaptureVideoPreviewLayer寫(xiě)的相機(jī)截屏如下

先從視頻流獲取圖片,繪制成圖片,再將view繪制上去,這其中view的不能設(shè)置背景色,否則相機(jī)部分是view的底色

```

-(void)cwCameraCaptureOutputSampleBuffer: (CMSampleBufferRef _Nullable)sampleBuffer bufferType: (NSInteger)bufferType{

? ? [self stopTimer:_cameraTimer];

? ? if(isStopLivess == NO){


? ? ? ? UIImage*image = [UIImagecw_getImageFromBGRA:sampleBufferbufferType:bufferType];

? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? ? ? [self snapScreenImage:image controller:self];

? ? ? ? });

? ? }

}

- (void)snapScreenImage:(UIImage*)imagecontroller:(UIViewController*)controller

{

? ? CGSizescreenSize = [[UIScreenmainScreen]bounds].size;

? ? UIGraphicsBeginImageContextWithOptions([[UIScreen mainScreen] bounds].size, NO, 0);

? ? CGContextRef context = UIGraphicsGetCurrentContext();

? ? UIGraphicsPushContext(context);


? ? CGFloatcameraScale =640/480.0;

? ? CGFloatcameraViewWidth =_camareView.cameraView.bounds.size.width;

? ? CGFloatcameraViewHeight =_camareView.cameraView.bounds.size.height;

//? ? CGFloat preLayerW = cameraViewWidth;

//? ? CGFloat preLayerH = preLayerW * cameraScale;

//? ? CGFloat preLayerY = -(preLayerH - cameraViewHeight) * 0.5;

? ? [image drawInRect:CGRectMake(CGRectGetMinX(_camareView.cameraView.frame), CGRectGetMinY(_camareView.cameraView.frame)-20, cameraViewWidth, image.size.height * cameraViewWidth/ image.size.width)];

? ? UIGraphicsPopContext();

?? [self renderView:_camareView inContext:context];

? ? UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();

? ? UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);

? ? UIGraphicsEndImageContext();

}

- (void)renderView:(UIView*)viewinContext:(CGContextRef)context

{

? ? // -renderInContext: renders in the coordinate space of the layer,

? ? // so we must first apply the layer's geometry to the graphics context

? ? CGContextSaveGState(context);

? ? // Center the context around the window's anchor point

? ? CGContextTranslateCTM(context, [view center].x, [view center].y);

? ? // Apply the window's transform about the anchor point

? ? CGContextConcatCTM(context, [view transform]);

? ? // Offset by the portion of the bounds left of and above the anchor point

? ? CGContextTranslateCTM(context,

? ? ? ? ? ? ? ? ? ? ? ? ? -[viewbounds].size.width* [[viewlayer]anchorPoint].x,

? ? ? ? ? ? ? ? ? ? ? ? ? -[viewbounds].size.height* [[viewlayer]anchorPoint].y);


? ? // Render the layer hierarchy to the current context

? ? [[viewlayer]renderInContext:context];

//? ? [view drawViewHierarchyInRect:view.frame afterScreenUpdates:NO];

? ? // Restore the context

? ? CGContextRestoreGState(context);

}

```

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容