zoukankan      html  css  js  c++  java
  • iOS开发之UIImage黑白处理

    //通过修改图片饱和度将图片变为黑白(镂空位置不影响)

    - (UIImage*) convertImageToGreyScale:(UIImage*) image

    {

        CIImage *beginImage = [CIImage imageWithCGImage:image.CGImage];

        CIFilter * filter = [CIFilter filterWithName:@"CIColorControls"];

        [filter setValue:beginImage forKey:kCIInputImageKey];

        //饱和度 0---2 默认为1

        [filter setValue:[NSNumber numberWithFloat:0] forKey:@"inputSaturation"];

        // 得到过滤后的图片

        CIImage *outputImage = [filter outputImage];

        // 转换图片, 创建基于GPU的CIContext对象

        CIContext *context = [CIContext contextWithOptions: nil];

        CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];

        UIImage *newImg = [UIImage imageWithCGImage:cgimg];

        // 释放C对象

        CGImageRelease(cgimg);

        return newImg;

    }

    //将图片变为黑边反转(镂空部位为黑色)

    - (UIImage*) convertImageToGreyScale:(UIImage*) image {

      CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);

      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

      CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);

      CGContextDrawImage(context, imageRect, [image CGImage]);

      CGImageRef imageRef = CGBitmapContextCreateImage(context);

      UIImage *newImage = [UIImage imageWithCGImage:imageRef];

      CGColorSpaceRelease(colorSpace); CGContextRelease(context);

      CFRelease(imageRef);

      return newImage;

    }

  • 相关阅读:
    需要学习的技术
    面试资料
    数据库设计三大范式
    java List 排序 Collections.sort() 对 List 排序
    hibernate的延迟加载
    索引失效原因总结
    mybatis调用oracle存储过程
    Android开发中需要注意哪些坑
    Intent在Activity之间传值的几种方式
    Android动画(Animations)
  • 原文地址:https://www.cnblogs.com/hecanlin/p/13468354.html
Copyright © 2011-2022 走看看