ios - 将矩阵转换为 UIImage

标签 ios objective-c matrix uiimage

我需要将表示黑白图像的矩阵转换为 UIImage。

例如:

像这样的矩阵(只是表示)。此图像将是符号“+”

1 0 1

0 0 0

1 0 1

这个矩阵表示黑白图像,其中黑色为 0,白色为 1。我需要将此矩阵转换为 UIImage。在这种情况下,宽度为 3,高度为 3

最佳答案

我使用这种方法为我的 Game Of Life 应用创建图像。与绘图到图形上下文相比的优点是速度快得离谱。

这都是很久以前写的,所以它比我现在可能做的有点困惑,但方法会保持不变。由于某些原因,我在方法之外定义了这些...

{
    unsigned int length_in_bytes;
    unsigned char *cells;
    unsigned char *temp_cells;
    unsigned char *changes;
    unsigned char *temp_changes;
    GLubyte *buffer;
    CGImageRef imageRef;
    CGDataProviderRef provider;
    int ar, ag, ab, dr, dg, db;
    float arf, agf, abf, drf, dgf, dbf, blah;
}

图像不需要所有这些。

方法本身...

- (UIImage*)imageOfMapWithDeadColor:(UIColor *)deadColor aliveColor:(UIColor *)aliveColor
{
    //translate colours into rgb components
    if ([deadColor isEqual:[UIColor whiteColor]]) {
        dr = dg = db = 255;
    } else if ([deadColor isEqual:[UIColor blackColor]]) {
        dr = dg = db = 0;
    } else {
        [deadColor getRed:&drf green:&dgf blue:&dbf alpha:&blah];

        dr = drf * 255;
        dg = dgf * 255;
        db = dbf * 255;
    }

    if ([aliveColor isEqual:[UIColor whiteColor]]) {
        ar = ag = ab = 255;
    } else if ([aliveColor isEqual:[UIColor blackColor]]) {
        ar = ag = ab = 0;
    } else {
        [aliveColor getRed:&arf green:&agf blue:&abf alpha:&blah];

        ar = arf * 255;
        ag = agf * 255;
        ab = abf * 255;
    }

//    dr = 255, dg = 255, db = 255;
//    ar = 0, ag = 0, ab = 0;

    //create bytes of image from the cell map
    int yRef, cellRef;

    unsigned char *cell_ptr = cells;

    for (int y=0; y<self.height; y++)
    {
        yRef = y * (self.width * 4);

        int x = 0;
        do
        {
            cellRef = yRef + 4 * x;

            if (*cell_ptr & 0x01) {
                //alive colour
                buffer[cellRef] = ar;
                buffer[cellRef + 1] = ag;
                buffer[cellRef + 2] = ab;
                buffer[cellRef + 3] = 255;
            } else {
                //dead colour
                buffer[cellRef] = dr;
                buffer[cellRef + 1] = dg;
                buffer[cellRef + 2] = db;
                buffer[cellRef + 3] = 255;
            }
            cell_ptr++;
        } while (++x < self.width);
    }

    //create image
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // render the byte array into an image ref
    imageRef = CGImageCreate(self.width, self.height, 8, 32, 4 * self.width, colorSpace, kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);

    // convert image ref to UIImage
    UIImage *image = [UIImage imageWithCGImage:imageRef];

    CGImageRelease(imageRef);
    CGColorSpaceRelease(colorSpace);

    //return image
    return image;
}

您应该能够对此进行调整以从您的矩阵创建图像。

关于ios - 将矩阵转换为 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26282194/

相关文章:

ios - 多次调用observeValueForKeyPath

r - 给定 n 个 4x4 矩阵的列表,我如何将它们组合起来,使它们连续跟随在一个 4nx4 矩阵中

ios - 如何从Firebase存储中删除照片

ios - Objective-C解析Json并检查响应是否是对象数组中的单个对象

Objective-C 运算符 (?) 和 ( :)

matrix - 使用 CUDA 进行动态矩阵乘法

matrix - Rust 特征 : The bounds might not be implemented, 并且我实现的特征不存在

iphone - iOS 5 "Notification Center"API?

ios - 如何清除/重置一对多关系中的所有CoreData

ios - iPad 上未显示应用程序图标