我参考了MetalKitEssentials,并使用iOS 9.1中的
我想从
我该怎么办?
谢谢阅读!
代码:
MetalKit
创建了模型查看器应用程序。我想从
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