Eva*_*ter 4 objective-c ios metal
我参考MetalKitEssentials,并通过MetalKitin制作模型查看器应用程序iOS 9.1。
我想从MTKView. 但是,我只得到黑色图像。
我该怎么办?
感谢您的阅读!
代码:
@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
Run Code Online (Sandbox Code Playgroud)
自解决。
我发现了一个类似的话题。
我为以下几点编写代码。
1:使用Objective-C
2:不使用扩展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);
}
Run Code Online (Sandbox Code Playgroud)
但是,我在 getBytes 方法中收到以下 exec 错误。
failed assertion `texture must not be a framebufferOnly texture.'
Run Code Online (Sandbox Code Playgroud)
此错误已在以下修复中修复。所以,我更改了 MTKView 的设置。
metalView.framebufferOnly = false;
Run Code Online (Sandbox Code Playgroud)
谢谢。
| 归档时间: |
|
| 查看次数: |
2537 次 |
| 最近记录: |