iOS 图片置灰(OC&Swift)

2021-06-07  本文已影响0人  iOS_我更专业

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

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

    }

上一篇 下一篇

猜你喜欢

热点阅读