美文网首页
iOS开发 通过文字生成图片,然后把图片转换为RGB

iOS开发 通过文字生成图片,然后把图片转换为RGB

作者: Leoeoo | 来源:发表于2019-10-29 10:07 被阅读0次
    一:通过文字生成图片
    // 通过文字生成图片
    + (UIImage *)imageWithTextStr:(NSString *)string font:(UIFont *)font width:(CGFloat)width textAlignment:(NSTextAlignment)textAlignment {
        NSDictionary *attributeDic = @{NSFontAttributeName:font};
        CGSize size = [string boundingRectWithSize:CGSizeMake(width, 10000)
                                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
                                        attributes:attributeDic
                                           context:nil].size;
        NSInteger retWidth = ceil(size.width);
        NSInteger retHeight = ceil(size.height);
        if (retWidth % 2 != 0) {
            retWidth++;
        }
        if (retHeight % 2 != 0) {
            retHeight++;
        }
        size = CGSizeMake(retWidth, retHeight);
        if ([UIScreen.mainScreen respondsToSelector:@selector(scale)]) {
            if (UIScreen.mainScreen.scale == 2.0) {
                UIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
            } else {
                UIGraphicsBeginImageContext(size);
            }
        } else {
            UIGraphicsBeginImageContext(size);
        }
        CGContextRef context = UIGraphicsGetCurrentContext();
        [[UIColor colorWithWhite:0 alpha:0] set];
        
        CGRect rect = CGRectMake(0, 0, retWidth, retHeight);
        CGContextFillRect(context, rect);
        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.alignment = textAlignment;
        NSDictionary *attributes = @{
                                     NSForegroundColorAttributeName:[UIColor whiteColor],
                                     NSFontAttributeName:font,
                                     NSParagraphStyleAttributeName:paragraph
                                     };
        [string drawInRect:rect withAttributes:attributes];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    二:图片转为RGB
    + (unsigned char *)rgbDataWithImage:(UIImage *)img {
        CGImageRef imgRef = [img CGImage];
        CGSize size = img.size;
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        int pixelCount = size.width * size.height;
        uint8_t* rgba = (uint8_t*)malloc(pixelCount * 4);
        CGContextRef context = CGBitmapContextCreate(rgba, size.width, size.height, 8, 4 * size.width, colorSpace, kCGImageAlphaPremultipliedLast);
        CGColorSpaceRelease(colorSpace);
        CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), imgRef);
        CGContextRelease(context);
        uint8_t *rgb = (uint8_t*)malloc(pixelCount * 3);
        int m = 0;
        int n = 0;
        /** 移除掉Alpha */
        for(int i=0; i<pixelCount; i++){
            rgb[m++] = rgba[n++];
            rgb[m++] = rgba[n++];
            rgb[m++] = rgba[n++];
            n++;
        }
        free(rgba);
        return rgb;
    }
    

    相关文章

      网友评论

          本文标题:iOS开发 通过文字生成图片,然后把图片转换为RGB

          本文链接:https://www.haomeiwen.com/subject/bedwvctx.html