CIImage大小范围为零?

时间:2016-11-17 12:45:09

标签: ios cgimage cifilter ciimage

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

-(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,无穷大,无穷大)...那里的数字确实很大。

显然处理后的图片为零。

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

1 个答案:

答案 0 :(得分:2)

使用以下代码,在他的回答William Vasconcelos中完全转到here,这就是我将其作为社区维基的原因:

//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