UIImage转BitMap并获取点阵数据

绝世酱油瓶 2018-11-12 04:14:05
如题,请教UIImage如何转成位图,并获取对应的点阵数据?
...全文
745 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_35206032 2019-09-02
  • 打赏
  • 举报
回复
引用 1 楼 dirdirdir3 的回复:
- (Byte *) getByteDataWithImage:(UIImage *)image {

    BOOL pol = self.dataPolarIndex == 1;

    NSInteger bit = 0 ;

    NSInteger addLeft = 0;

    NSInteger picWidth = image.size.width,picHeight = image.size.height;

    //  NSLog(@"图片款高---%ld----%ld",picWidth,picHeight);

    NSInteger shiftBit = 0;

    Byte r;

    Byte g;

    Byte b;

    NSMutableData *lastData = [[NSMutableData alloc]init];

    Byte          *lastByte;

    NSInteger x = _subtitleX;

    if (effectsRow2 == 1 || effectsRow2 == 2) {

        addLeft = 0;

    }else{

        addLeft = x % 8;

    }

    unsigned char *pixels = calloc(picWidth * picHeight * 4, sizeof(unsigned char)); // 取图片首地址//准备用来存储数据的数组

    size_t bitsPerComponent = 8; // 每个像素元素位数为 8 bit,即 rgba 每位各 1 个字节

    size_t bytesPerRow = picWidth *4; //一个像素 4 个字节,则一行共 4 * width 个字节

    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); // 创建rgb颜色空间 这决定了输出颜色的编码是 RGB 还是其他(比如 YUV)

    CGContextRef context =

    CGBitmapContextCreate(pixels,

                          picWidth,

                          picHeight,

                          bitsPerComponent,

                          bytesPerRow,

                          space,

                          kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGContextDrawImage(context, CGRectMake(0, 0, picWidth, picHeight), image.CGImage);//将图片的数据写入上下文



    CGColorSpaceRelease(space);

    CGContextRelease(context);



    NSInteger rgbSpace = ((picWidth + addLeft + 7)/8)  * picHeight;

    NSInteger grayLevel = 0;

    if ([[NSUserDefaults standardUserDefaults]integerForKey:@"selectGLRow"]) {

        grayLevel = [[NSUserDefaults standardUserDefaults]integerForKey:@"selectGLRow"];

    }else {

        grayLevel = 1;

    }

    for (NSInteger gray = 1; gray <= grayLevel; gray ++) {

        bit = 7 - (grayLevel - gray);

        unsigned char *rBytes = malloc(rgbSpace);

        unsigned char *gBytes = malloc(rgbSpace);

        unsigned char *bBytes = malloc(rgbSpace);

        NSInteger rgbStep = 0;

        for (NSInteger i = 0; i < picHeight; i ++) {

            r = 0;

            g = 0;

            b = 0;

            shiftBit = 8 - addLeft;

            for (NSInteger j = 0; j < picWidth; j ++) {

                r |= (((pixels[i*picWidth *4 +j*4 + 0] >> bit)&0x01) << (shiftBit -1));

                g |= (((pixels[i*picWidth *4 +j*4 + 1] >> bit)&0x01) << (shiftBit -1));

                b |= (((pixels[i*picWidth *4 +j*4 + 2] >> bit)&0x01) << (shiftBit -1));

                shiftBit --;

                if (shiftBit == 0 || (j==picWidth -1)) {

                    if (!pol) {

                        r = ~r;

                        g = ~g;

                        b = ~b;

                    }

                    rBytes[rgbStep] = r;

                    gBytes[rgbStep] = g;

                    bBytes[rgbStep] = b;



                    rgbStep++;

                    shiftBit = 8;

                    r = 0;

                    g = 0;

                    b = 0;

                }

            }

        }

        switch (self.colorIndex) {

            case 0:

                [lastData appendBytes:rBytes length:rgbSpace];

                break;

            case 1:

                switch (self.RGBOrder) {

                    case 0:

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        break;

                    case 1:

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:rBytes length:rgbSpace];

                        break;

                    default:

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        break;

                }

                break;

            case 2:

                switch (self.RGBOrder) {

                    case 2:

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:bBytes length:rgbSpace];

                        break;

                    case 3:

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:bBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        break;

                    case 4:

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:bBytes length:rgbSpace];

                        break;

                    case 5:

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:bBytes length:rgbSpace];

                        [lastData appendBytes:rBytes length:rgbSpace];

                        break;

                    case 6:

                        [lastData appendBytes:bBytes length:rgbSpace];

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        break;

                    case 7:

                        [lastData appendBytes:bBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:rBytes length:rgbSpace];

                        break;

                    default:

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:bBytes length:rgbSpace];

                        break;

                }

                break;

            default:

                [lastData appendBytes:rBytes length:rgbSpace];

                break;

        }

        free(rBytes);

        free(gBytes);

        free(bBytes);

        rBytes = NULL;

        bBytes = NULL;

        bBytes = NULL;

    }

    free(pixels);

    pixels = NULL;

    [[NSUserDefaults  standardUserDefaults] setInteger:lastData.length forKey:@"subTitleDataLength"];

    [[NSUserDefaults standardUserDefaults] synchronize];

    lastByte = (Byte *)[lastData bytes];

    return lastByte;

}

方法里面有些参数貌似是全局的,请问有完整的代码吗?或者demo
dirdirdir3 2018-11-26
  • 打赏
  • 举报
回复
- (Byte *) getByteDataWithImage:(UIImage *)image {

    BOOL pol = self.dataPolarIndex == 1;

    NSInteger bit = 0 ;

    NSInteger addLeft = 0;

    NSInteger picWidth = image.size.width,picHeight = image.size.height;

    //  NSLog(@"图片款高---%ld----%ld",picWidth,picHeight);

    NSInteger shiftBit = 0;

    Byte r;

    Byte g;

    Byte b;

    NSMutableData *lastData = [[NSMutableData alloc]init];

    Byte          *lastByte;

    NSInteger x = _subtitleX;

    if (effectsRow2 == 1 || effectsRow2 == 2) {

        addLeft = 0;

    }else{

        addLeft = x % 8;

    }

    unsigned char *pixels = calloc(picWidth * picHeight * 4, sizeof(unsigned char)); // 取图片首地址//准备用来存储数据的数组

    size_t bitsPerComponent = 8; // 每个像素元素位数为 8 bit,即 rgba 每位各 1 个字节

    size_t bytesPerRow = picWidth *4; //一个像素 4 个字节,则一行共 4 * width 个字节

    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); // 创建rgb颜色空间 这决定了输出颜色的编码是 RGB 还是其他(比如 YUV)

    CGContextRef context =

    CGBitmapContextCreate(pixels,

                          picWidth,

                          picHeight,

                          bitsPerComponent,

                          bytesPerRow,

                          space,

                          kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGContextDrawImage(context, CGRectMake(0, 0, picWidth, picHeight), image.CGImage);//将图片的数据写入上下文



    CGColorSpaceRelease(space);

    CGContextRelease(context);



    NSInteger rgbSpace = ((picWidth + addLeft + 7)/8)  * picHeight;

    NSInteger grayLevel = 0;

    if ([[NSUserDefaults standardUserDefaults]integerForKey:@"selectGLRow"]) {

        grayLevel = [[NSUserDefaults standardUserDefaults]integerForKey:@"selectGLRow"];

    }else {

        grayLevel = 1;

    }

    for (NSInteger gray = 1; gray <= grayLevel; gray ++) {

        bit = 7 - (grayLevel - gray);

        unsigned char *rBytes = malloc(rgbSpace);

        unsigned char *gBytes = malloc(rgbSpace);

        unsigned char *bBytes = malloc(rgbSpace);

        NSInteger rgbStep = 0;

        for (NSInteger i = 0; i < picHeight; i ++) {

            r = 0;

            g = 0;

            b = 0;

            shiftBit = 8 - addLeft;

            for (NSInteger j = 0; j < picWidth; j ++) {

                r |= (((pixels[i*picWidth *4 +j*4 + 0] >> bit)&0x01) << (shiftBit -1));

                g |= (((pixels[i*picWidth *4 +j*4 + 1] >> bit)&0x01) << (shiftBit -1));

                b |= (((pixels[i*picWidth *4 +j*4 + 2] >> bit)&0x01) << (shiftBit -1));

                shiftBit --;

                if (shiftBit == 0 || (j==picWidth -1)) {

                    if (!pol) {

                        r = ~r;

                        g = ~g;

                        b = ~b;

                    }

                    rBytes[rgbStep] = r;

                    gBytes[rgbStep] = g;

                    bBytes[rgbStep] = b;



                    rgbStep++;

                    shiftBit = 8;

                    r = 0;

                    g = 0;

                    b = 0;

                }

            }

        }

        switch (self.colorIndex) {

            case 0:

                [lastData appendBytes:rBytes length:rgbSpace];

                break;

            case 1:

                switch (self.RGBOrder) {

                    case 0:

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        break;

                    case 1:

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:rBytes length:rgbSpace];

                        break;

                    default:

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        break;

                }

                break;

            case 2:

                switch (self.RGBOrder) {

                    case 2:

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:bBytes length:rgbSpace];

                        break;

                    case 3:

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:bBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        break;

                    case 4:

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:bBytes length:rgbSpace];

                        break;

                    case 5:

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:bBytes length:rgbSpace];

                        [lastData appendBytes:rBytes length:rgbSpace];

                        break;

                    case 6:

                        [lastData appendBytes:bBytes length:rgbSpace];

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        break;

                    case 7:

                        [lastData appendBytes:bBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:rBytes length:rgbSpace];

                        break;

                    default:

                        [lastData appendBytes:rBytes length:rgbSpace];

                        [lastData appendBytes:gBytes length:rgbSpace];

                        [lastData appendBytes:bBytes length:rgbSpace];

                        break;

                }

                break;

            default:

                [lastData appendBytes:rBytes length:rgbSpace];

                break;

        }

        free(rBytes);

        free(gBytes);

        free(bBytes);

        rBytes = NULL;

        bBytes = NULL;

        bBytes = NULL;

    }

    free(pixels);

    pixels = NULL;

    [[NSUserDefaults  standardUserDefaults] setInteger:lastData.length forKey:@"subTitleDataLength"];

    [[NSUserDefaults standardUserDefaults] synchronize];

    lastByte = (Byte *)[lastData bytes];

    return lastByte;

}

29,049

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧