3
我參考MetalKitEssentials,並通過MetalKit
在iOS 9.1
中進行模型查看器應用。如何從iOS中的MTKView製作屏幕截圖圖像?
我想從MTKView
製作截屏圖像(RGBA格式)。 但是,我只得到黑色的圖像。
我該怎麼辦?
謝謝您的閱讀!
代碼:
@implemtntation MetalViewController
{
MTKView *metalView;
}
- (void)viewDidLoad
{
[super viewDidload];
[self setup];
}
- (void)setup
{
metalDevice = MTLCreateSystemDefaultDevice();
commandQueue = [metalDevice newCommandQueue];
defaultLibrary = [metalDevice newDefaultLibrary];
metalView = [[MTKView alloc] initWithFrame:self.view.frame];
[self.view addSubview:metalView];
metalView.delegate = self;
metalView.device = metalDevice;
metalView.sampleCount = 4;
metalView.depthStencilPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
metalView.opaque = false;
metalView.framebufferOnly = true;
// and more set up for Metal ...
}
// if you make screen shot, call this method.
- (IBAction)onCapture:(id)sender
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContextWithOptions(screenRect.size, YES, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFillRect(context, screenRect);
// draw image to context
[metalView.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, self,
@selector(completeSavedImage:didFinishSavingWithError:contextInfo:), nil);
}
- (void)completeSavedImage:(UIImage *)_image didFinishSavingWithError:(NSError *)_error contextInfo:(void *)_contextInfo
{
if (!_error)
{
NSLog(@"ok");
}
else
{
NSLog(@"error");
}
}
@end