美文网首页
改变图片背景色的4种方法

改变图片背景色的4种方法

作者: 携一两本单色书来 | 来源:发表于2018-10-24 15:51 被阅读88次

    方法1 CIFilter 滤镜

    适合纯色背景,扣图精准

    struct CubeMap cubeMap =  createCubeMap(60, 90);
        NSData *data = [NSData dataWithBytesNoCopy:cubeMap.data length:cubeMap.length freeWhenDone:YES];
        CIFilter *colorCubeFilter = [CIFilter filterWithName:@"CIColorCube"];
         
        [colorCubeFilter setValue:[NSNumber numberWithFloat:cubeMap.dimension] forKey:@"inputCubeDimension"];
        [colorCubeFilter setValue:data forKey:@"inputCubeData"];
        [colorCubeFilter setValue:[[CIImage alloc] initWithImage:_iv.image] forKey:kCIInputImageKey];
        CIImage *outputImage = colorCubeFilter.outputImage;
        
        CIFilter *sourceOverCompositingFilter = [CIFilter filterWithName:@"CISourceOverCompositing"];
        
        [sourceOverCompositingFilter setValue:outputImage forKey:kCIInputImageKey];
        [sourceOverCompositingFilter setValue:[[CIImage alloc] initWithImage:[UIImage imageNamed:@"background"]] forKey:kCIInputBackgroundImageKey];
        
        outputImage = sourceOverCompositingFilter.outputImage;
        CIContext  *context = [CIContext new];
        CGImageRef cgImage = [context createCGImage:outputImage fromRect:outputImage.extent];
        if (cgImage) {
            _iv.image = [UIImage imageWithCGImage:cgImage];
        }
    

    方法2 像素点遍历

    判断每一个像素点的颜色,如果在范围内修改点

    - (UIImage *)removeWhiteColorWithImage:(UIImage *)image{
        return [self removeColorWithMaxR:255 minR:250 maxG:255 minG:250 maxB:255 minB:250 image:image];
    }
    - (UIImage *)removeColorWithMaxR:(float)maxR minR:(float)minR maxG:(float)maxG minG:(float)minG maxB:(float)maxB minB:(float)minB image:(UIImage *)image{
        // 分配内存
        const int imageWidth = image.size.width;
        const int imageHeight = image.size.height;
        size_t bytesPerRow = imageWidth * 4;
        uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
    
        // 创建context
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();// 色彩范围的容器
        CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
        CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);
    
    
        // 遍历像素
        int pixelNum = imageWidth * imageHeight;
        uint32_t* pCurPtr = rgbImageBuf;
        for (int i = 0; i < pixelNum; i++, pCurPtr++)
        {
            uint8_t* ptr = (uint8_t*)pCurPtr;
            if (ptr[3] >= minR && ptr[3] <= maxR &&
                ptr[2] >= minG && ptr[2] <= maxG &&
                ptr[1] >= minB && ptr[1] <= maxB &&
                ptr[0] >0.5) {
                ptr[0] = 0;
                DBLog(@"白色变透明");
            }else{
                printf("\n---->ptr0:%d ptr1:%d ptr2:%d ptr3:%d<----\n",ptr[0],ptr[1],ptr[2],ptr[3]);
            }
        }
        // 将内存转成image
        CGDataProviderRef dataProvider =CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, nil);
        CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight,8, 32, bytesPerRow, colorSpace,kCGImageAlphaLast |kCGBitmapByteOrder32Little, dataProvider,NULL,true,kCGRenderingIntentDefault);
        CGDataProviderRelease(dataProvider);
        UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef];
    
        // 释放
        CGImageRelease(imageRef);
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        return resultUIImage;
    }
    
    

    方法3 CGImageCreateWithMaskingColors

    Creates a bitmap image by masking an existing bitmap image with the provided color values. 要掩盖的颜色值

     +(UIImage *(^)(UIImage *img))TransparentBackgroundImg{
        return ^UIImage *(UIImage *img){
             //    const float colorMasking[6] = {minR, maxR,  minG, maxG, minB, maxB};
             const CGFloat colorMasking[6] = {250.0, 255.0, 250.0, 255.0, 250.0, 255.0};
             CGImageRef sourceImage = img.CGImage;
              CGImageAlphaInfo info = CGImageGetAlphaInfo(sourceImage);
             if (info != kCGImageAlphaNone) {
                 NSData *buffer = UIImageJPEGRepresentation(img, 1);
                 UIImage *newImage = [UIImage imageWithData:buffer];
                 sourceImage = newImage.CGImage;
             }
             
             CGImageRef masked = CGImageCreateWithMaskingColors(sourceImage, colorMasking);
             UIImage *retImage = [UIImage imageWithCGImage:masked];
             CGImageRelease(masked);
             return retImage;
             
            };
        
    }
    

    调用的时候直接调用:

    [self.bankImg sd_setImageWithURL:urlStr(bill.organizationImg) completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    image = UoFactoryTool.TransparentBackgroundImg(image);
                    //image = [self removeWhiteColorWithImage:image];
                    image = [image imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate];
                    self.bankImg.image = image;
                    self.bankImg.tintColor = [UIColor colorWithHexString:@"D8DADE"];
                    
                    
                }];
    

    方法4 找设计师,简单性价比高

    Graph Cut Grab Cut 待尝试

    相关文章

      网友评论

          本文标题:改变图片背景色的4种方法

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