Making a UIImageView grey

后端 未结 4 941
离开以前
离开以前 2021-01-12 03:35

I\'m setting UImageViews within table cells using setImageWithUrl from the AFNetworking library, but I need the images to be greyscale... is there any way I can do this. I\'

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-12 04:26

    This thread is a bit old but I came across it on my search. In this post I've pointed out 2 different methods in Swift to create a grayscale image that can be displayed in an imageView considering the alpha and scale of the image.

    import CoreImage
    
    extension UIImage
    {
        /// Applies grayscale with CIColorControls by settings saturation to 0.0.
        /// - Parameter brightness: Default is 0.0.
        /// - Parameter contrast: Default is 1.0.
        /// - Returns: The grayscale image of self if available.
        func grayscaleImage(brightness: Double = 0.0, contrast: Double = 1.0) -> UIImage?
        {
            if let ciImage = CoreImage.CIImage(image: self, options: nil)
            {
                let paramsColor: [String : AnyObject] = [ kCIInputBrightnessKey: NSNumber(double: brightness),
                                                          kCIInputContrastKey:   NSNumber(double: contrast),
                                                          kCIInputSaturationKey: NSNumber(double: 0.0) ]
                let grayscale = ciImage.imageByApplyingFilter("CIColorControls", withInputParameters: paramsColor)
    
                let processedCGImage = CIContext().createCGImage(grayscale, fromRect: grayscale.extent)
                return UIImage(CGImage: processedCGImage, scale: self.scale, orientation: self.imageOrientation)
            }
            return nil
        }
    
        /// Create a grayscale image with alpha channel. Is 5 times faster than grayscaleImage().
        /// - Returns: The grayscale image of self if available.
        func convertToGrayScale() -> UIImage?
        {
            // Create image rectangle with current image width/height * scale
            let pixelSize = CGSize(width: self.size.width * self.scale, height: self.size.height * self.scale)
            let imageRect = CGRect(origin: CGPointZero, size: pixelSize)
            // Grayscale color space
            if let colorSpace: CGColorSpaceRef = CGColorSpaceCreateDeviceGray()
            {
                // Create bitmap content with current image size and grayscale colorspace
                let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.None.rawValue)
                if let context: CGContextRef = CGBitmapContextCreate(nil, Int(pixelSize.width), Int(pixelSize.height), 8, 0, colorSpace, bitmapInfo.rawValue)
                {
                    // Draw image into current context, with specified rectangle
                    // using previously defined context (with grayscale colorspace)
                    CGContextDrawImage(context, imageRect, self.CGImage)
                    // Create bitmap image info from pixel data in current context
                    if let imageRef: CGImageRef = CGBitmapContextCreateImage(context)
                    {
                        let bitmapInfoAlphaOnly = CGBitmapInfo(rawValue: CGImageAlphaInfo.Only.rawValue)
                        if let contextAlpha = CGBitmapContextCreate(nil, Int(pixelSize.width), Int(pixelSize.height), 8, 0, nil, bitmapInfoAlphaOnly.rawValue)
                        {
                            CGContextDrawImage(contextAlpha, imageRect, self.CGImage)
                            if let mask: CGImageRef = CGBitmapContextCreateImage(contextAlpha)
                            {
                                // Create a new UIImage object
                                if let newCGImage = CGImageCreateWithMask(imageRef, mask)
                                {
                                    // Return the new grayscale image
                                    return UIImage(CGImage: newCGImage, scale: self.scale, orientation: self.imageOrientation)
                                }
                            }
                        }
                    }
                }
    
            }
            // A required variable was unexpected nil
            return nil
        }
    }
    

提交回复
热议问题