Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (id<MTLTexture>) createTextureFromImage:(UIImage*) image device:(id<MTLDevice>) device
- {
- image =[UIImage imageWithCGImage:[image CGImage]
- scale:[image scale]
- orientation: UIImageOrientationLeft];
- NSLog(@"orientation and size and stuff %ld %f %f", (long)image.imageOrientation, image.size.width, image.size.height);
- CGImageRef imageRef = image.CGImage;
- 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(@"%@ %u", colorSpace, alphaInfo);
- 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 = MTLPixelFormatRGBA8Unorm;
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement