当前位置:  开发笔记 > IOS > 正文

区域覆盖相同的像素颜色并进行更改

如何解决《区域覆盖相同的像素颜色并进行更改》经验,为你挑选了0个好方法。

如何擦除用相同颜色覆盖的特定像素和像素区域的颜色.我能够使用下面的快速代码获得特定的像素颜色,但是我无法获得具有相同颜色像素且无法擦除它的周围区域.

import UIKit

class ColorOfImage: UIImageView {
    
    var lastColor:UIColor? = nil


    
    /*
     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
     }
     */
    
    override func touchesEnded(_ touches: Set, with event: UIEvent?) {
        
        if self.isHidden == true {
            
            self.next?.touchesEnded(touches, with: event)
            return
        }
        
        let touch: UITouch = touches.first!
        
        var point:CGPoint = touch.location(in: self)
        self.lastColor = self.getPixelColorAtLocation(point:point)  
    }
    
    
    public func createARGBBitmapContext(inImage: CGImage) -> CGContext {

        var bitmapByteCount = 0
        var bitmapBytesPerRow = 0
        
        //Get image width, height
        let pixelsWide = inImage.width
        let pixelsHigh = inImage.height
        
        // Declare the number of bytes per row. Each pixel in the bitmap in this
        // example is represented by 4 bytes; 8 bits each of red, green, blue, and
        // alpha.
        bitmapBytesPerRow = Int(pixelsWide) * 4
        bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)
        
        // Use the generic RGB color space.
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        
        
        
        // Allocate memory for image data. This is the destination in memory
        // where any drawing to the bitmap context will be rendered.
        let bitmapData = malloc(bitmapByteCount)
        
        // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
        // per component. Regardless of what the source image format is
        // (CMYK, Grayscale, and so on) it will be converted over to the format
        // specified here by CGBitmapContextCreate.
        let context = CGContext(data: bitmapData, width: pixelsWide, height: pixelsHigh, bitsPerComponent: 8, bytesPerRow: bitmapBytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue)
        
        // Make sure and release colorspace before returning
        return context!
    }
    
    
    
    
    public func getPixelColorAtLocation( point:CGPoint) -> UIColor {
        // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
        var point = point
        
        var context:CGContext? = nil
        
        context = self.createARGBBitmapContext(inImage: (self.image?.cgImage)!)
        
        
        if context == nil {
            return UIColor.white
        }
        
        var pixelsWide = (self.image?.cgImage)!.width
        var pixelsHigh = (self.image?.cgImage)!.height
        var rect = CGRect(x:0, y:0, width:Int(pixelsWide), height:Int(pixelsHigh))
        
        var xScale:CGFloat = CGFloat(pixelsWide)/self.frame.size.width
        var yScale:CGFloat = CGFloat(pixelsHigh)/self.frame.size.height
        
        
        point.x = point.x * xScale
        point.y = point.y * yScale
        
        
        
        var x:CGFloat = 1.0
        
        
        
        if (self.image?.responds(to: #selector(getter:  self.image?.scale)))! {
            x =  ( self.image!.scale)
            
        }
        
        
        
        //Clear the context
        context?.clear(rect)
        
        // Draw the image to the bitmap context. Once we draw, the memory
        // allocated for the context for rendering will then contain the
        // raw image data in the specified color space.
        
        context?.draw((self.image?.cgImage)!, in: rect)
        
        // Now we can get a pointer to the image data associated with the bitmap
        // context.
        
        
        let data = context?.data
        //    let dataType = UnsafePointer(data)
        
        var color:UIColor? = nil
        if data != nil {
            let dataType = data?.assumingMemoryBound(to: UInt8.self)
            
            
            let offset = 4*((Int(pixelsWide) * Int(point.y)) + Int(point.x))
            let alpha = dataType?[offset]
            let red = dataType?[offset+1]
            let green = dataType?[offset+2]
            let blue = dataType?[offset+3]
            color = UIColor(red: CGFloat(red!)/255.0, green: CGFloat(green!)/255.0, blue: CGFloat(blue!)/255.0, alpha: CGFloat(alpha!)/255.0)
        }
        else
        {
            
            
        }
        
        
        
        // Free image data memory for the context
        free(data)
        return color!;
    } 
}

例如,如果我触摸UIImage和特定坐标,那么它必须得到颜色并擦除那个具有相同颜色的像素和周围像素,并且所有那些都必须擦除.可以帮助我.

推荐阅读
小妖694_807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有