iOS 图片(UIImage)变暗的一种方法

demo地址:ImageDark

直接贴代码:

typedef enum {
    ALPHA = 0,
    BLUE = 1,
    GREEN = 2,
    RED = 3
} PIXELS;

@implementation UIImage (ImageDarken)

-(UIImage *)imageDarkenWithLevel:(float) level
{
    level = level > 1.0 ? 1.0 : level;
    level = level < 0.0 ? 0.0 : level;
    
    CGSize size = [self size];
    int width = size.width;
    int height = size.height;
    
    // 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), [self CGImage]);
    
    
    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
            
            // darken the pixels
            rgbaPixel[RED] = rgbaPixel[RED] * level;
            rgbaPixel[GREEN] = rgbaPixel[GREEN] * level;
            rgbaPixel[BLUE] = rgbaPixel[BLUE] * level;
        }
    }
    
    // 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);
    
    // make a new UIImage to return
    UIImage *resultUIImage = [UIImage imageWithCGImage:image];
    
    // we're done with image now too
    CGImageRelease(image);
    
    return resultUIImage;

}

调用:

 UIImage *sourceImage = [UIImage imageNamed:@"timg"];
    UIImageView *view0 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height/2 - 20)];
    [view0 setImage:sourceImage];
    [self.view addSubview:view0];
    
    UIImageView *view1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20 + self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2 - 20)];
    view1.image = [sourceImage imageDarkenWithLevel:0.4];
    [self.view addSubview:view1];

效果如图:

iOS 图片(UIImage)变暗的一种方法_第1张图片
30A6F61B-65F0-45E9-A0E0-58491B794D58.png

你可能感兴趣的:(iOS 图片(UIImage)变暗的一种方法)