---
title: "How to Convert matrix to UIImage?"  
description: "How to Convert matrix to UIImage?"  
author: "Pravesh Singh"  
published: 2014-10-17  
updated: 2014-10-17  
canonical: https://www.mindstick.com/forum/2412/how-to-convert-matrix-to-uiimage  
category: "iphone"  
tags: ["iphone", "ios"]  
reading_time: 2 minutes  

---

# How to Convert matrix to UIImage?

I need to [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) a [matrix](https://answers.mindstick.com/qa/47113/how-is-the-movie-the-matrix-reloaded) representing a b/w [image](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) to UIImage.

For example:

A matrix like this (just the representation). This image would be the symbol +'

001

110

0101

This matrix represents an image in [black](https://yourviews.mindstick.com/view/83102/badi-elaichi-black-cardamom-health-benefits) and white, where black is 0 and white is 1. I need to convert this matrix to UIImage. In this case width would be 3 and height would be 3

## Replies

### Reply by Anonymous User

In order to convert a matrix to UIImage :\

```
CGSize size = CGSizeMake(lines, columns);UIGraphicsBeginImageContextWithOptions(size, YES, 0);for (int i = 0; i < lines; i++){    for (int j = 0; j < columns; j++)    {        // Choose color to draw         if ( matrixDraw[i*lines + j] == 1 ) {            [[UIColor whiteColor] setFill];        } else {            // Draw black pixel            [[UIColor blackColor] setFill];                    }        // Draw just one pixel in i,j        UIRectFill(CGRectMake(i, j, 1, 1));     }}// Create UIImage with the current context that we have just createdUIImage *imageFinal = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
```

Basically what we are doing is :\
**Create a context with the size of our image**\
Looping for each pixel to see the value. Black is 0 and white is 1. So depends on the value, we set the color.\
**The most important function :** UIRectFill(CGRectMake(i,j,1,1));This function let us to fill a pixel in the i,j position with width and height (1 both cases for fill one single pixel)\
Finally we create an UIImage with the current context and we call to finish the image context.Hope it helps someone!


---

Original Source: https://www.mindstick.com/forum/2412/how-to-convert-matrix-to-uiimage

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
