我正在使用CIPixellate
過濾器進行一些測試,我已使用該工具,但生成的圖像大小各不相同。我認爲這是有道理的,因爲我正在改變輸入的比例,但並不是我所期待的 - 我認爲它會在圖像的矩形內進行縮放。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;
}
不知道這是否會幫助,但嘗試的NSNumber numberWithFloat爲輸入標定 – Roecrew 2013-05-02 21:25:33