本文介绍了如何在iOS中通过MTKView制作屏幕快照图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我引用了 MetalKitEsssentials ,并通过iOS 9.1
中的MetalKit
创建了模型查看器应用程序.
I refer to MetalKitEssentials, and make model viewer application by MetalKit
in iOS 9.1
.
我想从MTKView
制作屏幕快照图像(RGBA格式).但是,我只得到黑色图像.
I want to make screen shot image(RGBA Format) from MTKView
.But, I get only black image.
我该怎么办?
感谢您的阅读!
代码:
@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
推荐答案
自我解决.
我发现了一个类似的主题.
我为以下几点写代码.
1:使用Objective-C
1: using Objective-C
2:不使用扩展名MTLTexture
2: no using extension MTLTexture
- (IBAction)onCapture:(id)sender
{
id<MTLTexture> lastDrawableDisplayed = [metalView.currentDrawable texture];
int width = (int)[lastDrawableDisplayed width];
int height = (int)[lastDrawableDisplayed height];
int rowBytes = width * 4;
int selfturesize = width * height * 4;
void *p = malloc(selfturesize);
[lastDrawableDisplayed getBytes:p bytesPerRow:rowBytes fromRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaFirst;
CGDataProviderRef provider = CGDataProviderCreateWithData(nil, p, selfturesize, nil);
CGImageRef cgImageRef = CGImageCreate(width, height, 8, 32, rowBytes, colorSpace, bitmapInfo, provider, nil, true, (CGColorRenderingIntent)kCGRenderingIntentDefault);
UIImage *getImage = [UIImage imageWithCGImage:cgImageRef];
CFRelease(cgImageRef);
free(p);
NSData *pngData = UIImagePNGRepresentation(getImage);
UIImage *pngImage = [UIImage imageWithData:pngData];
UIImageWriteToSavedPhotosAlbum(pngImage, self,
@selector(completeSavedImage:didFinishSavingWithError:contextInfo:), nil);
}
但是,我在getBytes方法中遇到以下exec错误.
But, I get the following exec error in getBytes method.
failed assertion `texture must not be a framebufferOnly texture.'
此错误已在以下修复程序中修复.因此,我更改了MTKView的设置.
This error has been fixed in the following fix.So, I change setting for MTKView.
metalView.framebufferOnly = false;
谢谢.
这篇关于如何在iOS中通过MTKView制作屏幕快照图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!