CIImage 大小范围为零?

CIImage size extent zero?

我已创建此类别以将 CIImage 转换为 UIImage。我使用的 CIImages 来自 CIFilters.

-(UIImage *)obtainImageWithRetinaScale:(CGFloat)scale
{
  CIContext *context = [CIContext contextWithOptions:nil];

  //CGRect frame = CGRectMake(0,0,700,392);
  CGImageRef processedCGImage = [context createCGImage:self
                                              fromRect:[self extent]];

  UIImage *returnImage = [UIImage imageWithCGImage:processedCGImage
                                    scale:scale
                              orientation:UIImageOrientationUp];
  CGImageRelease(processedCGImage);

  return returnImage;
}

当它运行时,[self extent] 给出一个带有乱码的矩形,几乎没有 (0, 0, infinity, infinity) ...那里的数字非常大。

明显处理后的图片为零

当我强制使用正确尺寸的帧时,图像会正确生成。

使用以下代码,功劳完全归于William Vasconcelos in his answer here,这就是我将其设为社区 wiki 的原因:

//assume beginImage is CIImage you want to tint            
CIImage* outputImage = nil;

//create some blue
CIFilter* blueGenerator = [CIFilter filterWithName:@"CIConstantColorGenerator"];
CIColor* blue = [CIColor colorWithString:@"0.1 0.5 0.8 1.0"];
[blueGenerator setValue:blue forKey:@"inputColor"];
CIImage* blueImage = [blueGenerator valueForKey:@"outputImage"];

//apply a multiply filter
CIFilter* filterm = [CIFilter filterWithName:@"CIMultiplyCompositing"];
[filterm setValue:blueImage forKey:@"inputImage"];
[filterm setValue:beginImage forKey:@"inputBackgroundImage"];
outputImage = [filterm valueForKey:@"outputImage"];

为了带来一些个人贡献并使其值得回答,这里是 Swift 3 版本:

// assume beginImage is the CIImage you want to tint
let outputImage: CIImage?
let blueGenerator = CIFilter(name: "CIConstantColorGenerator", withInputParameters: ["inputColor": CIColor(string: "0.1 0.5 0.8 1.0")])
let blueImage = blueGenerator!.outputImage

let filterM = CIFilter(name: "CIMultiplyCompositing", withInputParameters: ["inputImage": blueImage, "inputBackgroundImage": beginImage])
outputImage = filterM.outputImage