CIBumpDistortion 滤镜的使用

2020-08-14  本文已影响0人  sergeant

CIBumpDistortion 滤镜效果如下

滤镜效果

先看参数:

CIFilter *filter = [CIFilter filterWithName:@"CIBumpDistortion"];
NSLog(@"%@ - %@", filterName, filter.attributes);
    inputCenter =     {
        CIAttributeClass = CIVector;
        CIAttributeDefault = "[150 150]";
        CIAttributeDescription = "The center of the effect as x and y coordinates.";
        CIAttributeDisplayName = Center;
        CIAttributeType = CIAttributeTypePosition;
    };
    inputImage =     {
        CIAttributeClass = CIImage;
        CIAttributeDescription = "The image to use as an input image. For filters that also use a background image, this is the foreground image.";
        CIAttributeDisplayName = Image;
        CIAttributeType = CIAttributeTypeImage;
    };
    inputRadius =     {
        CIAttributeClass = NSNumber;
        CIAttributeDefault = 300;
        CIAttributeDescription = "The radius determines how many pixels are used to create the distortion. The larger the radius, the wider the extent of the distortion.";
        CIAttributeDisplayName = Radius;
        CIAttributeMin = 0;
        CIAttributeSliderMax = 600;
        CIAttributeSliderMin = 0;
        CIAttributeType = CIAttributeTypeDistance;
    };
    inputScale =     {
        CIAttributeClass = NSNumber;
        CIAttributeDefault = "0.5";
        CIAttributeDescription = "The scale of the effect determines the curvature of the bump. A value of 0.0 has no effect. Positive values create an outward bump; negative values create an inward bump.";
        CIAttributeDisplayName = Scale;
        CIAttributeIdentity = 0;
        CIAttributeSliderMax = 1;
        CIAttributeSliderMin = "-1";
        CIAttributeType = CIAttributeTypeScalar;
    };

所以这个滤镜除了image以外还需要以下参数:

实例

代码

    CIFilter *filter = [CIFilter filterWithName:@"CIBumpDistortion"];
    CIContext *context = [CIContext contextWithOptions:nil];
    NSLog(@"%@ - %@", filterName, filter.attributes);
    if (filter.attributes[kCIInputImageKey]) {
        [filter setValue:inputImage forKey:kCIInputImageKey];
        
        if (filter.attributes[kCIInputCenterKey]) {
            [filter setValue:[CIVector vectorWithX:self.image.size.width / 2 Y:self.image.size.height / 2] forKey:kCIInputCenterKey];
        }
        
        if (filter.attributes[kCIInputRadiusKey]) {
            [filter setValue:@(50) forKey:kCIInputRadiusKey];
        }
        
        if (filter.attributes[kCIInputScaleKey]) {
            [filter setValue:@(0.8) forKey:kCIInputScaleKey]; // 外凸
            // [filter setValue:@(0.8) forKey:kCIInputScaleKey]; // 内凹
        }

        CIImage *outPutImage = filter.outputImage;
        CGImageRef imageRef = [context createCGImage:outPutImage fromRect:outPutImage.extent];
        if (imageRef) {
            return [UIImage imageWithCGImage:imageRef];
        }
    }
    
    return nil;

效果

外凸 内凹
上一篇 下一篇

猜你喜欢

热点阅读