很多文章都提到是普通view或者layer的截屏方法俄认,就不再重復(fù)
針對特殊的?AVCaptureVideoPreviewLayer寫的相機截屏如下
先從視頻流獲取圖片往毡,繪制成圖片蒙揣,再將view繪制上去,這其中view的不能設(shè)置背景色开瞭,否則相機部分是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);
}
```