ios - 使用 MTKView 显示 JPEG 图像
<p><p>有没有办法通过 MTKView 和 MTLBuffer(以及在 iPhone 6+ 中)显示 JPEG 图像。我已经通过以下方式进行了尝试(这只是测试):</p>
<pre><code>- (id<MTLBuffer>)testBuffer
{
if (!_testBuffer)
{
//
NSString *path = [ pathForResource:@"testImage" ofType:@"jpg"];
NSData *imageData = ;
_testBuffer = ;
}
return _testBuffer;
}
- (void)drawInMTKView:(MTKView *)view
{
MTLRenderPassDescriptor* renderPassDescriptor = view.currentRenderPassDescriptor;
id <MTLCommandBuffer> commandBuffer = ;
id <MTLRenderCommandEncoder> renderEncoder = ;
;
;
;
;
}
</code></pre>
<p>我在此方法调用中遇到错误:</p>
<pre><code>;
</code></pre>
<blockquote>
<p>/BuildRoot/Library/Caches/com.apple.xbs/Sources/Metal/Metal-54.31/ToolsLayers/Debug/MTLDebugRenderCommandEncoder.mm:2123:
failed assertion `-[MTLDebugRenderCommandEncoder
drawPrimitives:indirectBuffer:indirectBufferOffset:] is only supported
on MTLFeatureSet_iOS_GPUFamily3_v1 and later'</p>
</blockquote>
<p>这是因为只有 <a href="https://developer.apple.com/library/ios/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/MetalFeatureSetTables/MetalFeatureSetTables.html#//apple_ref/doc/uid/TP40014221-CH13-SW1" rel="noreferrer noopener nofollow">iPhone 6s(+)</a> 支持此 API . </p>
<p>而且我认为我做错了什么。也许我需要换个方向思考。有人可以帮助我或指出正确的方向吗?
谢谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>没有简单的方法可以使用 MTLBuffer 通过 MTKView 呈现图像数据。在这种情况下,您必须为正确的输出纹理准备正确的像素表示,该输出纹理用于由 MTKView 在可绘制层中显示。最简单的方法是使用 MTKTextureLoader,或者从 UIImage/CGImage 实例加载数据到 MTLTexture 对象。</p>
<p>我做了一个类似的实验,使用 MTL 层和 MTLTexture(而不是 MTLBuffer)在 iOS 设备上呈现图像,我已经决定了显示图像的最佳方式(如果只需要:显示图像)它使用直通无需渲染的内核函数。</p>
<p>在 Swift2 中,如下所示:</p>
<pre><code>...
let textureLoader = MTKTextureLoader(device: self.device!)
if let image = UIImage(named: file){
imageTexture = try! textureLoader.newTextureWithCGImage(image.CGImage!, options: nil)
threadGroups = MTLSizeMake(
(imageTexture.width+threadGroupCount.width)/threadGroupCount.width,
(imageTexture.height+threadGroupCount.height)/threadGroupCount.height, 1)
}
}
...
let function:MTLFunction! = library.newFunctionWithName("kernel_passthrough")
...
pipeline = try! self.device.newComputePipelineStateWithFunction(function)
...
let commandBuffer = commandQueue.commandBuffer()
let encoder = commandBuffer.computeCommandEncoder()
encoder.setComputePipelineState(pipeline)
encoder.setTexture(actualImageTexture, atIndex: 0)
encoder.setTexture(metalView.currentDrawable!.texture, atIndex: 1)
encoder.setBuffer(self.saturationUniform, offset: 0, atIndex: 0)
encoder.dispatchThreadgroups(threadGroups!, threadsPerThreadgroup: threadGroupCount)
encoder.endEncoding()
commandBuffer.presentDrawable(metalView.currentDrawable!)
commandBuffer.commit()
...
</code></pre>
<p>在 Metal 着色文件中:
</p>
<pre><code>kernel void kernel_passthrough(texture2d<float, access::read> inTexture [],
texture2d<float, access::write> outTexture [],
uint2 gid [])
{
float4 inColor = inTexture.read(gid);
//
// flip texture vertically if it needs to display with right orientation
//
outTexture.write(inColor, gid);
}
</code></pre>
<p>完整示例来源:<a href="https://github.com/dnevera/ImageMetalling/tree/master/ImageMetalling-00" rel="noreferrer noopener nofollow">ImageMetalling-00</a> </p></p>
<p style="font-size: 20px;">关于ios - 使用 MTKView 显示 JPEG 图像,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33605241/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33605241/
</a>
</p>
页:
[1]