我正在用CIPixellate过滤器做一些测试,我让它正常工作,但是产生的图像大小不同。我认为这是合理的,因为我正在改变输入刻度,但这不是我所期望的-我以为它会在图像的正向范围内缩放。
我是误解了/使用了过滤器,还是只需要将输出图像裁剪到我想要的大小。
另外,从头/试和错误来看,inputCenter参数对我来说并不清楚。有人能解释一下这是怎么回事吗?
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;
}发布于 2017-09-03 18:45:43
问题是只对进行缩放。
只需做:
let result = UIImage(cgImage: cgimgresult!, scale: (originalImageView.image?.scale)!, orientation: (originalImageView.image?.imageOrientation)!)
originalImageView.image = result发布于 2019-04-24 09:10:49
默认情况下,模糊滤波器还通过模糊图像像素以及(在滤波器的图像处理空间中)包围图像的透明像素来软化图像的边缘。
所以你的输出图像会根据你的比例而变化。
对于inputCenter,正如约书亚·沙利文在这个在CIFilter上发布的注释中提到的,“它从源图像中调整像素网格的偏移量”。因此,如果inputCenter坐标不是CI像素inputScale的倍数,它将略微抵消像素平方(大部分在inputScale的大值上可见)。
发布于 2015-04-22 03:37:52
有时,inputScale会不均匀地分割您的图像,这是当我发现我得到不同大小的输出图像时。
例如,如果inputScale =0或1,则输出图像大小非常精确。
我发现图像周围的额外空间居中的方式因inputCenter的“不透明”而异。呃,我还没花时间弄清楚到底是怎么回事(我把它通过点击位置设置在视图中)。
我对不同尺寸的解决方案是将图像重新渲染成输入图像的大小,这样做时我会为Apple Watch提供一个黑色的背景。
CIFilter *pixelateFilter = [CIFilter filterWithName:@"CIPixellate"];
[pixelateFilter setDefaults];
[pixelateFilter setValue:[CIImage imageWithCGImage:editImage.CGImage] forKey:kCIInputImageKey];
[pixelateFilter setValue:@(amount) forKey:@"inputScale"];
[pixelateFilter setValue:vector forKey:@"inputCenter"];
CIImage* result = [pixelateFilter valueForKey:kCIOutputImageKey];
CIContext *context = [CIContext contextWithOptions:nil];
CGRect extent = [pixelateResult extent];
CGImageRef cgImage = [context createCGImage:result fromRect:extent];
UIGraphicsBeginImageContextWithOptions(editImage.size, YES, [editImage scale]);
CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ref, 0, editImage.size.height);
CGContextScaleCTM(ref, 1.0, -1.0);
CGContextSetFillColorWithColor(ref, backgroundFillColor.CGColor);
CGRect drawRect = (CGRect){{0,0},editImage.size};
CGContextFillRect(ref, drawRect);
CGContextDrawImage(ref, drawRect, cgImage);
UIImage* filledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnImage = filledImage;
CGImageRelease(cgImage);如果您要坚持您的实现,我建议您至少改变提取UIImage的方式,以使用原始图像的“比例”,而不是将其与CIFilter标度混淆。
UIImage *newImage = [UIImage imageWithCGImage:cgiimage scale:fromImage.scale orientation:fromImage.imageOrientation];https://stackoverflow.com/questions/15425437
复制相似问题