CIBumpDistortion 滤镜的使用

CIBumpDistortion 滤镜效果如下

CIBumpDistortion 滤镜的使用_第1张图片
滤镜效果

先看参数:

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以外还需要以下参数:

  • inputCenter:中心点,默认值[150, 150]
  • inputRadius:半径,默认值300,范围0~600
  • inputScale:曲率,默认值0.5,范围-1~1,正直外凸,负值内凹

实例

代码

    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;

效果

CIBumpDistortion 滤镜的使用_第2张图片
外凸
CIBumpDistortion 滤镜的使用_第3张图片
内凹

你可能感兴趣的:(CIBumpDistortion 滤镜的使用)