ios - CIPixellate 图像输出大小变化

标签 ios core-image cifilter

我正在使用 CIPixellate 滤镜进行一些测试,它可以正常工作,但生成的图像大小不同。我认为这是有道理的,因为我正在改变输入比例,但这不是我所期望的——我认为它会在图像的矩形范围内缩放。

我是误会/使用了错误的滤镜,还是只需要将输出图像裁剪成我想要的大小。

此外,inputCenter 参数对我来说从阅读 header /试验和错误中不清楚。谁能解释一下这个参数是什么意思?

NSMutableArray * tmpImages = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
    double scale = i * 4.0;
    UIImage* tmpImg = [self applyCIPixelateFilter:self.faceImage withScale:scale];
    printf("tmpImg    width: %f height: %f\n",  tmpImg.size.width, tmpImg.size.height);
    [tmpImages addObject:tmpImg];
}

tmpImg    width: 480.000000 height: 640.000000
tmpImg    width: 484.000000 height: 644.000000
tmpImg    width: 488.000000 height: 648.000000
tmpImg    width: 492.000000 height: 652.000000
tmpImg    width: 496.000000 height: 656.000000
tmpImg    width: 500.000000 height: 660.000000
tmpImg    width: 504.000000 height: 664.000000
tmpImg    width: 508.000000 height: 668.000000
tmpImg    width: 512.000000 height: 672.000000
tmpImg    width: 516.000000 height: 676.000000

- (UIImage *)applyCIPixelateFilter:(UIImage*)fromImage withScale:(double)scale
{
    /*
     Makes an image blocky by mapping the image to colored squares whose color is defined by the replaced pixels.
     Parameters

     inputImage: A CIImage object whose display name is Image.

     inputCenter: A CIVector object whose attribute type is CIAttributeTypePosition and whose display name is Center.
     Default value: [150 150]

     inputScale: An NSNumber object whose attribute type is CIAttributeTypeDistance and whose display name is Scale.
     Default value: 8.00
     */
    CIContext *context = [CIContext contextWithOptions:nil];
    CIFilter *filter= [CIFilter filterWithName:@"CIPixellate"];
    CIImage *inputImage = [[CIImage alloc] initWithImage:fromImage];
    CIVector *vector = [CIVector vectorWithX:fromImage.size.width /2.0f Y:fromImage.size.height /2.0f];
    [filter setDefaults];
    [filter setValue:vector forKey:@"inputCenter"];
    [filter setValue:[NSNumber numberWithDouble:scale] forKey:@"inputScale"];
    [filter setValue:inputImage forKey:@"inputImage"];

    CGImageRef cgiimage = [context createCGImage:filter.outputImage fromRect:filter.outputImage.extent];
    UIImage *newImage = [UIImage imageWithCGImage:cgiimage scale:1.0f orientation:fromImage.imageOrientation];

    CGImageRelease(cgiimage);

    return newImage;
}

最佳答案

问题仅在于缩放

简单地做:

let result = UIImage(cgImage: cgimgresult!, scale: (originalImageView.image?.scale)!, orientation: (originalImageView.image?.imageOrientation)!)
            originalImageView.image = result

关于ios - CIPixellate 图像输出大小变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15425437/

相关文章:

ios - Core Image 和 GPUImage 之间的主要区别

ios - CIFilter似乎在GPU上失败

ios - Xamarin IOS 中的自定义 CIColorKernel

arkit - 如何使用 ARMatteGenerator 将 CIFilter 添加到 MTLTexture?

ios - NSLayoutAttribute中的Top和TopMargin有什么区别?

ios - 我如何知道用户是否点击了按钮?

swift - 在 UI 元素上应用 CIFilter

ios - 当应用程序处于后台时,CIFilter 无法正常工作

iphone - 有哪些很棒的 iPhone 练习题?

ios - 找不到包含我的保存数据的文件