美文网首页
iOS图片取色

iOS图片取色

作者: iOS丶Michelle | 来源:发表于2022-03-17 22:32 被阅读0次

效果

1.gif

用到的第三方

SOZOChromoplast

主要代码

#import "SOZOBitmapDataGenerator.h"

@implementation SOZOBitmapDataGenerator

static void *bitmapData;

//获取图片内颜色信息
+ (NSArray *)bitmapDataForImage:(UIImage *)image {
    NSMutableArray *array = [NSMutableArray new];
    unsigned char* cArray = [self rawBitmapDataForImage:image];
    NSInteger numberOfPixels = (NSInteger) [image size].width * [image size].height;
    UIColor *color;
    for (int i = 0; i < numberOfPixels * 4; i += 4) {
        color = [UIColor colorWithRed:cArray[i+1]/255.f
                                green:cArray[i+2]/255.f
                                 blue:cArray[i+3]/255.f
                                alpha:cArray[i]/255.f];
        [array addObject:color];
    }
    free(bitmapData);
    return array;
}

+ (unsigned char *)rawBitmapDataForImage:(UIImage *)image {
    CGContextRef contextRef = [self ARGBContextWithSize:image.size];
    if (!contextRef) {
        return NULL;
    }

    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    CGContextDrawImage(contextRef, rect, image.CGImage);
    unsigned char *data = CGBitmapContextGetData(contextRef);
    CGContextRelease(contextRef);

    return data;
}

+ (CGContextRef)ARGBContextWithSize:(CGSize)size {
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    if (!colorSpace) {
        [NSException raise:NSInternalInconsistencyException format:@"Error creating color space."];
    }

    NSInteger bytesPerPixel = 4;
    unsigned long bytesPerRow = size.width * bytesPerPixel;
    unsigned long totalBytes = bytesPerRow * size.height;

    bitmapData = malloc(totalBytes);
    if (!bitmapData) {
        [NSException raise:NSInternalInconsistencyException format:@"Memory allocation error."];
    }

    // Cast to silence a type mismatch warning.
    // From the docs: The constants for specifying the alpha channel information are declared
    // with the `CGImageAlphaInfo` type but can be passed to [the bitmapInfo] parameter safely.
    CGBitmapInfo alphaSettings = (CGBitmapInfo) kCGImageAlphaPremultipliedFirst;
    CGContextRef contextRef = CGBitmapContextCreate(bitmapData, size.width, size.height, 8,
                                                    bytesPerRow, colorSpace, alphaSettings);
    if (!contextRef) {
        [NSException raise:NSInternalInconsistencyException format:@"Error creating context."];
    }

    CGColorSpaceRelease(colorSpace);
    return contextRef;
}


@end

Swift如何使用

Pod安装一下,然后import SOZOChromoplast,这个库里面的dominantColor感觉取出的颜色不是我们肉眼看上去所感觉的。

//MARK: 使用OC库,实现取色,非常好用
    func useSOZOChromoplast(){
        let chromoplast = SOZOChromoplast(image: self.imageVs[self.PageControl.currentPage].image!)
        self.view.backgroundColor = chromoplast?.secondHighlight  //主色调
        print("---图片里总共有几种颜色-----\(String(describing: chromoplast?.colors.count))")
//      chromoplast?.colors[item] = UIExtendedSRGBColorSpace 0.7 0.546078 0.488235 1
        var itemcolor:UIColor
        for i in 0 ..< paletteViews.count {
            if let tempcolor = chromoplast?.colors[i] as? UIColor{
                itemcolor = tempcolor
                self.paletteViews[i].backgroundColor = itemcolor
                self.paletteLabels[i].text = "getPalette[\(i)] R\(itemcolor.rgbvalue.red) G\(itemcolor.rgbvalue.green) B\(itemcolor.rgbvalue.blue)"
            }
        }
    }

另一种是纯Swift实现的

这个感觉比较迟钝,有点慢,图片进行切换了,颜色还没取出,但是它可以取出我们肉眼所感觉的主色调。

ColorThiefSwift

相关文章

网友评论

      本文标题:iOS图片取色

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