IT虾米网

iOS处理图片颜色代码详解

sanshao 2018年06月24日 手机开发 370 0
处理图片颜色 
- (UIImage*)grayscale:(UIImage*)anImage type:(int)type{ 
    CGImageRef imageRef = anImage.CGImage; 
    size_t width  = CGImageGetWidth(imageRef); 
    size_t height = CGImageGetHeight(imageRef); 
    size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef); 
    size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef); 
    size_t bytesPerRow = CGImageGetBytesPerRow(imageRef); 
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef); 
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 
    bool shouldInterpolate = CGImageGetShouldInterpolate(imageRef); 
    CGColorRenderingIntent intent = CGImageGetRenderingIntent(imageRef); 
    CGDataProviderRef dataProvider = CGImageGetDataProvider(imageRef); 
    CFDataRef data = CGDataProviderCopyData(dataProvider); 
    UInt8 *buffer = (UInt8*)CFDataGetBytePtr(data); 
    NSUInteger  x, y; 
    for (y = 0; y < height; y++) { 
        for (x = 0; x < width; x++) { 
            UInt8 *tmp; 
            tmp = buffer + y * bytesPerRow + x * 4; 
            UInt8 red,green,blue; 
            red = *(tmp + 0); 
            green = *(tmp + 1); 
            blue = *(tmp + 2); 
            UInt8 brightness; 
              
            switch (type) { 
                case 1: 
                    brightness = (77 * red + 28 * green + 151 * blue) / 256; 
                    *(tmp + 0) = brightness; 
                    *(tmp + 1) = brightness; 
                    *(tmp + 2) = brightness; 
                    break; 
                case 2: 
                    *(tmp + 0) = red; 
                    *(tmp + 1) = green * 0.7; 
                    *(tmp + 2) = blue * 0.4; 
                    break; 
                case 3: 
                    *(tmp + 0) = 255 - red; 
                    *(tmp + 1) = 255 - green; 
                    *(tmp + 2) = 255 - blue; 
                    break; 
                default: 
                    *(tmp + 0) = red; 
                    *(tmp + 1) = green; 
                    *(tmp + 2) = blue; 
                    break; 
            } 
  
        } 
    } 
    CFDataRef effectedData = CFDataCreate(NULL, buffer, CFDataGetLength(data)); 
    CGDataProviderRef effectedDataProvider = CGDataProviderCreateWithCFData(effectedData); 
    CGImageRef effectedCgImage = CGImageCreate( 
                                               width, height, 
                                               bitsPerComponent, bitsPerPixel, bytesPerRow, 
                                               colorSpace, bitmapInfo, effectedDataProvider, 
                                               NULL, shouldInterpolate, intent); 
    UIImage *effectedImage = [[UIImage alloc] initWithCGImage:effectedCgImage]; 
    CGImageRelease(effectedCgImage); 
    CFRelease(effectedDataProvider); 
    CFRelease(effectedData); 
    CFRelease(data); 
    return effectedImage; 
}

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

fmdb常用操作代码详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。