我有這樣的方法MTLTexture顯示器UIImages錯誤如果轉換爲紋理
- (id<MTLTexture>) createTextureFromImage:(UIImage*) image device:(id<MTLDevice>) device
{
CGImageRef imageRef = image.CGImage;
// size_t width = CGImageGetWidth(imageRef);
// size_t height = CGImageGetHeight(imageRef);
size_t width = self.view.frame.size.width;
size_t height = self.view.frame.size.height;
size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
NSLog(@"%@", colorSpace);
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | alphaInfo;
NSLog(@"bitmap info %u", bitmapInfo);
CGContextRef context = CGBitmapContextCreate(NULL, width, height, bitsPerComponent, (bitsPerPixel/8) * width, colorSpace, bitmapInfo);
if(!context)
{
NSLog(@"Failed to load image, probably an unsupported texture type");
return nil;
}
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);
MTLPixelFormat format = MTLPixelFormatBGRA8Unorm;
MTLTextureDescriptor *texDesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:format
width:width
height:height
mipmapped:NO];
id<MTLTexture> texture = [device newTextureWithDescriptor:texDesc];
[texture replaceRegion:MTLRegionMake2D(0, 0, width, height)
mipmapLevel:0
withBytes:CGBitmapContextGetData(context)
bytesPerRow:4 * width];
return texture;
}
當我通過它的圖像,並使用所得到的紋理,它給人的整體圖像的紅色色調。我的MTLRenderPipelineDescriptor的像素格式設置爲MTLPixelFormatBGRA8Unorm,我感覺問題在這裏。我附上了一張圖片樣本,讓您知道它應該是什麼,以及它顯示的是什麼。我知道這不是圖像,就好像我只是將圖像直接添加到視圖效果良好,但這也可能是因爲視圖使用RGBA8顏色值,而metalview使用的是BGRA8顏色值。如果我將像素格式更改爲RGBA8不過,我的應用程序崩潰,並警告
For color attachment 0, the render pipeline's pixelFormat (MTLPixelFormatRGBA8Unorm) does not match the framebuffer's pixelFormat (MTLPixelFormatBGRA8Unorm).
的唯一的事情是,我不能看到我設置的幀緩衝像素格式。
謝謝!
編輯 - 這是我的圖像經過一次處理代碼後,我將它分成像素數組。
-(UIImage *) script:(NSArray*)pixelArray {
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
//create drawing context
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
int k = 0;
do {
for (int y = 0; y < height + 1; y++)
{
for (int x = 1; x < width; x++)
{
if (k < array.count){
UIColor* pixelColor = array[k];
CGContextSetFillColorWithColor(context, pixelColor.CGColor);
CGContextFillRect(context, CGRectMake(x, y, 1.0f, 1.0f));
}
} else {
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, CGRectMake(x, y, 1.0f, 1.0f));
}
k++;
}
k++;
}
} while (k < 62791);
//capture resultant image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
你需要支持哪些最低的iOS版本?你的目標只是從圖像創建MTLTexture? – curious
是的,我的目標是從圖像中創建一個MTLTexture,正如你所看到的那樣。問題出在RGBA頻道正在切換(我認爲) – crashlog
我只是想說,你可以用較少的代碼實現相同的目標。如果您打算僅支持9+ iOS,您可以使用MetalKit中的MTKTextureLoader,如[此處](http://stackoverflow.com/a/34895822/3014519)(viewDidLoad方法)所述。 – curious