ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

灰度图片和灰度颜色

2021-05-08 19:34:11  阅读:193  来源: 互联网

标签:gray 颜色 mm self 灰度 sourceImage UIImage pixels 图片


+ (UIImage *)convertToGrayscale:(UIImage*)sourceImage {
    return [self convertToGrayscale:sourceImage.CGImage scale:sourceImage.scale];
}

+ (UIImage *)convertToGrayscale:(CGImageRef)sourceImage scale:(CGFloat)scale {
    if (!sourceImage) {
        return nil;
    }
    
    UIImage *resultUIImage = nil;
    
    @autoreleasepool {
        int width = (int)CGImageGetWidth(sourceImage);
        int height = (int)CGImageGetHeight(sourceImage);

        // the pixels will be painted to this array
        uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

        // clear the pixels so any transparency is preserved
        memset(pixels, 0, width * height * sizeof(uint32_t));

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

        // create a context with RGBA pixels
        CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
                                                     kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

        // paint the bitmap to our context which will fill in the pixels array
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), sourceImage);

        for(int y = 0; y < height; y++) {
            for(int x = 0; x < width; x++) {
                uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];

                // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
                uint8_t gray = (uint8_t)round(0.299 * rgbaPixel[1] + 0.587 * rgbaPixel[2] + 0.114 * rgbaPixel[3]);
                // set the pixels to gray
                rgbaPixel[1] = gray;
                rgbaPixel[2] = gray;
                rgbaPixel[3] = gray;
            }
        }

        // create a new CGImageRef from our context with the modified pixels
        CGImageRef image = CGBitmapContextCreateImage(context);

        // we're done with the context, color space, and pixels
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        free(pixels);
        resultUIImage = [UIImage imageWithCGImage:image
                                                     scale:scale
                                               orientation:UIImageOrientationUp];

        // we're done with image now too
        CGImageRelease(image);
    }

    // make a new UIImage to return
    
    return resultUIImage;
}
@interface CALayer (CustomerColorsLayer)


@end


@implementation CALayer (CustomerColorsLayer)


+ (void)load {
//    [self ms_swizzleMethod:@selector(setContents:) withMethod:@selector(mm_setContents:)];
//    [self ms_swizzleMethod:@selector(drawInContext:) withMethod:@selector(mm_drawInContext:)];
}

- (void)mm_setContents:(id)contents {
    static CFTypeID imageType = 0;
    if (imageType == 0) {
        imageType = CFGetTypeID([UIImage imageWithColor:[UIColor whiteColor]].CGImage);
    }
    if (contents != NULL && CFGetTypeID((__bridge CFTypeRef)(contents)) == imageType) {
        @autoreleasepool {
            UIImage *sourceImage = [UIImage imageWithCGImage:(__bridge CGImageRef _Nonnull)(contents)];
            UIImage *grayImage = [UIImage convertToGrayscale:sourceImage];
            [self mm_setContents:(__bridge id)grayImage.CGImage];
        }
    } else {
        [self mm_setContents:contents];
    }
    
    
}


- (void)mm_drawInContext:(CGContextRef)ctx {
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextSetFillColorSpace(ctx, colorSpace);
    [self mm_drawInContext:ctx];
    CGColorSpaceRelease(colorSpace);
}

@end

@interface UIImageView (CustomerColorsLayer)

@end

@implementation UIImageView (CustomerColorsLayer)

+ (void)load {
    [self ms_swizzleMethod:@selector(setImage:) withMethod:@selector(mm_setImage:)];
}

- (void)mm_setImage:(UIImage *)image {
    [self mm_setImage:[UIImage convertToGrayscale:image]];
}

@end


@interface UIColor (CustomerColorsLayer)

@end


@implementation UIColor (CustomerColorsLayer)

+ (void)load {
    [self ms_swizzleClassMethod:@selector(colorWithRed:green:blue:alpha:) withMethod:@selector(grayColorWithRed:green:blue:alpha:)];
}

+ (UIColor *)grayColorWithRed:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a {
    CGFloat gray = r * 0.299 +g * 0.587 + b * 0.114;
    UIColor *grayColor = [UIColor grayColorWithRed:gray green:gray blue:gray alpha:a];
    return  grayColor;
}

@end

 

标签:gray,颜色,mm,self,灰度,sourceImage,UIImage,pixels,图片
来源: https://www.cnblogs.com/yuxiaoyiyou/p/14746212.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有