美文网首页
iOS 图片置灰(OC&Swift)

iOS 图片置灰(OC&Swift)

作者: iOS_我更专业 | 来源:发表于2021-06-07 10:50 被阅读0次

对于项目中需要图片置灰的处理方法:

OC:

//将图片置灰

+ (UIImage*)grayScale:(UIImage*)sourceImage{

   int bitmapInfo = kCGImageAlphaPremultipliedLast;

   int width = sourceImage.size.width;

   int height = sourceImage.size.height;

   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

   CGContextRef context =CGBitmapContextCreate(nil,width,height,8,0,colorSpace,bitmapInfo);

   CGColorSpaceRelease(colorSpace);

   if(context ==NULL) {

       return nil;

   }

   CGContextDrawImage(context,CGRectMake(0,0, width, height), sourceImage.CGImage);

   UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];

   CGContextRelease(context);

   return grayImage;

}

Swift:

// 图片置灰 偏弱

    func grayScale() ->UIImage?{

        guard let imageCG = self.cgImage else{

            return nil

        }

        let width = imageCG.width

        let height = imageCG.height

        let colorSpace = CGColorSpaceCreateDeviceRGB()

        // 申请内存空间

        let pixels =UnsafeMutablePointer.allocate(capacity: width*height )

        //UInt32在计算机中所占的字节

        let uint32Size =MemoryLayout.size

        let context =CGContext.init(data: pixels,

                                width: width,

                                height: height,

                                bitsPerComponent:8,

                                bytesPerRow: uint32Size*width,

                                space: colorSpace,

                                bitmapInfo:CGBitmapInfo.byteOrder32Little.rawValue|CGImageAlphaInfo.premultipliedLast.rawValue)

        context?.draw(imageCG, in:CGRect(x:0, y:0, width: width, height: height))

        for in 0..<height {

            for in 0..<width {

                let rgbaPixel = pixels.advanced(by: y*width+x)

                //类型转换 -> UInt8

                let rgb = unsafeBitCast(rgbaPixel, to:UnsafeMutablePointer.self)

                // rgba 所在位置 alpha 0, blue  1, green 2, red 3

                let gray =UInt8(0.3  *Double(rgb[3])+

                                 0.59*Double(rgb[2])+

                                 0.11*Double(rgb[1]))

                rgb[3] = gray

                rgb[2] = gray

                rgb[1] = gray

            }

        }

        guard let image = context?.makeImage()else{

            return nil

        }

        pixels.deallocate()

        return UIImage(cgImage: image, scale:0, orientation:self.imageOrientation)

    }

推荐使用
func grayScale() ->UIImage?{

        let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue

        let width =self.size.width

        let height =self.size.height

        let colorSpace = CGColorSpaceCreateDeviceGray()

        if let context =CGContext(data:nil, width:Int(width), height:Int(height), bitsPerComponent:8, bytesPerRow:0, space: colorSpace, bitmapInfo: bitmapInfo){

            if let cgImg = self.cgImage{

                context.draw(cgImg, in:CGRect.init(x:0, y:0, width: width, height: height))

                if let contextCGImg = context.makeImage(){

                    let grayImage =UIImage(cgImage: contextCGImg)

                    return grayImage

                }

            }

        }

        return nil

    }

相关文章

  • iOS 图片置灰(OC&Swift)

    对于项目中需要图片置灰的处理方法: OC: //将图片置灰+ (UIImage*)grayScale:(UIIma...

  • iOS图片置灰

    通过对图片属性的操作,得到灰色图片赋值

  • 图片置灰

    + (UIImage *)grayImage:(UIImage *)sourceImage { CGColorSp...

  • Shader图片置灰

    参考:NGUI Sprite效果变灰(按钮禁用状态)的解决方案 非ScrollView下使用 shader就是在片...

  • imageView图片置灰

    adapter里加载数据动态置灰背景图片 将ImageView变成灰色 ColorMatrix matrix = ...

  • 图片批量置灰

    // GrayPic.cpp : This file contains the 'main' function. ...

  • 交互-按钮置灰与不置灰

    理了下按钮置灰还是不置灰的逻辑,总结讲就是简单的置灰,复杂的不置灰单项的置灰,多项的不置灰 简单的置灰,单项的置灰...

  • 图片的置灰与复原

    置灰: 效果: 复原:

  • android view置灰(哀悼日)

    置灰前 置灰后 给Activity的顶层View设置置灰,实现全局置灰效果。获取界面的根View:

  • 置灰

    视觉置灰 视觉置灰在界面设计中主要起到的是一个筛选的作用。以豆瓣和虎扑为例,帖子如果浏览过就会置灰。 在电商类的产...

网友评论

      本文标题:iOS 图片置灰(OC&Swift)

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