HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 区域覆盖相同的像素颜色并进行更改大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何擦除用相同颜色覆盖的特定像素和像素区域的颜色.我能够使用下面的快速代码获得特定的像素颜色,但是我无法获得具有相同颜色像素且无法擦除它的周围区域.
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<UITouch>,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<UInt8>(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并在特定坐标处,那么它必须得到颜色并擦除那个具有相同颜色的像素和周围像素,并且所有那些都必须擦除.可以请帮助我.

解决方法

一个月前,我在一个应用程序工作,你得到一个图像,你不得不交换颜色……我不知道它是否对你有用.
我为它制作了一个UIImage类别.

无论如何这里是Objective-C中的代码

#import <UIKit/UIKit.h>

@interface UIImage (YPKInterfacE)
+ (UIImage *)swapImage:(UIImage *)image color:(UIColor *)originalColor withColor:(UIColor *)swappedColor andThreshold:(float)threshold;
@end

#import "UIImage+YPKInterface.h"

@implementation UIImage (YPKInterfacE)

+ (UIImage *)swapImage:(UIImage *)image color:(UIColor *)originalColor     withColor:(UIColor *)swappedColor andThreshold:(float)threshold {

int count = image.size.width*image.size.height;

// Convert image in raw data
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4,sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData,width,height,bitsPerComponent,bytesPerRow,colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGColorSpaceRelease(colorSpacE);

CGContextDrawImage(context,CGRectMake(0,height),imageRef);
CGContextRelease(context);

// Scan all the pixels
NSUInteger byteIndex = 0;
for (int i = 0 ; i < count ; ++i)
{
    CGFloat alpha = ((CGFloat) rawData[byteIndex + 3] ) / 255.0f;
            CGFloat red   = ((CGFloat) rawData[byteIndex]     ) / alpha;
            CGFloat green = ((CGFloat) rawData[byteIndex + 1] ) / alpha;
            CGFloat blue  = ((CGFloat) rawData[byteIndex + 2] ) / alpha;

    // Change the color

    // RGBA of the colors
    float origR = CGColorGetComponents(originalColor.CGColor)[0] * 255;
    float origG = CGColorGetComponents(originalColor.CGColor)[1] * 255;
    float origB = CGColorGetComponents(originalColor.CGColor)[2] * 255;
    //float origA = CGColorGetComponents(originalColor.CGColor)[3];

    float swapR = CGColorGetComponents(swappedColor.CGColor)[0] * 255;
    float swapG = CGColorGetComponents(swappedColor.CGColor)[1] * 255;
    float swapB = CGColorGetComponents(swappedColor.CGColor)[2] * 255;
    //float swapA = CGColorGetComponents(swappedColor.CGColor)[3];

    if (red >= origR - threshold && red <= origR + threshold &&
        green >= origG - threshold && green <= origG + threshold &&
        blue >= origB - threshold && blue <= origB + threshold) {
        rawData[byteIndex + 3] = alpha * 255;
        rawData[byteIndex    ] = swapR * alpha;
        rawData[byteIndex + 1] = swapG * alpha;
        rawData[byteIndex + 2] = swapB * alpha;
    }

    byteIndex += bytesPerPixel;
}

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,rawData,width*height*4,null);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

CGBitmapInfo bitmapInfo =
kCGBitmapByteOrderDefault | kCGImageAlphaLast;

CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef2 = CGImageCreate(width,8,32,4*width,colorSpaceRef,bitmapInfo,provider,NULL,NO,renderingIntent);

UIImage *newImage = [UIImage imageWithCGImage:imageRef2];

return newImage;
}

@end

大佬总结

以上是大佬教程为你收集整理的ios – 区域覆盖相同的像素颜色并进行更改全部内容,希望文章能够帮你解决ios – 区域覆盖相同的像素颜色并进行更改所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。