How to create a texture from UIimage

Hi there,
I purchased the book to learn basics of Metal to solve one of my code’s problem. It is great book but unfortunately I could not find the answer. All the texture examples in the book is uploaded. I am curious whether we can convert a UIimage to a texture data. I want to grab some portion of a UIimage pixels with colors and extent it to entire UIimage to get the color pattern distributed. I thought I can do it by using Metal but I could not see any example whether it is possible to create a texture from UIimage pixels. Any idea whether this is possible?

So your texture is not a file on disk? It’s held in a UIImage?

This might work for you:

You can get a CGImage from UIImage(named: "image").cgImage for example.

There’s also a good texture article from Apple here: Apple Developer Documentation

This is the sort of code that you’ll need to capture color data:

thanks Caroline,
Right my texture is not a file. I want to create a texture from UIimage to copy some portion of it. Since I am new in coding I have difficulty to understand Apple documents without sample codes. I try to use examples and modify it for my purposes. This way is much faster to learn. Is it possible for you to create a GitHub repository with more samples on Metal? There is not enough and some of them is written in Objective C not in Swift even in Apple sample codes. This repo can be accessible to whom purchased the book if it is no entirely open source. May be people who purchases the book can contribute to with their codes.

Thank you for the feedback and the idea