无法从CIImage获取图像数据

时间:2016-11-29 10:13:28

标签: ios objective-c cifilter ciimage

我正在使用CIFilterUIImageJPEGRepresentation进行图像过滤。之后,我试图通过CIImage *beginImage = [[CIImage alloc] initWithImage:sourceImage]; CIContext *context = [CIContext contextWithOptions:nil]; CIFilter *filter = [CIFilter filterWithName:effect keysAndValues:kCIInputImageKey, beginImage, nil]; CIImage *outputImage = [filter outputImage]; CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; UIImage *filteredImage = [UIImage imageWithCIImage:outputImage]; CGImageRelease(cgimg); NSData *imageData = UIImageJPEGRepresentation(filteredImage, 1.0); 获取图像数据,但我得到的是空数据。

#content-wrapper {
  background-color: #EBEBEB;
  margin: 0 auto;
  width: 100%;
  border-top: 2.5px solid #0F0F0F;
}
#content-wrapper ul {
  list-style-type: none;
  margin: 0 auto;
  width: 100%;
  padding: 0;
  text-align: center;
}
#content-wrapper ul li {
  background-color: whitesmoke;
  color: #1B1B1B;
  text-align: center;
  display: inline-block;
  margin: 5px 2.5px;
  width: 45%;
}
#content-wrapper ul li p {
  padding: 10px;
  height: 150px;
}
#content-wrapper ul li img {
  width: 100%;
}

我在控制imageData时收到空数据。请检查我的上述代码,并告诉我我在哪里做错了。

2 个答案:

答案 0 :(得分:2)

您的问题就在这里,您正在通过l()

CIImage

但是你需要在创建UIImage *filteredImage = [UIImage imageWithCIImage:outputImage]; 时传递CGImageRef来写这个

UIImage

我希望它能帮到你

答案 1 :(得分:1)

//创建我们模糊的图像试试这个希望它适合你。

 CIContext *context = [CIContext contextWithOptions:nil];
 CIImage *inputImage = [CIImage imageWithCGImage:theImage.CGImage]; //theImage pass your image
 CIFilter *filter1 = [CIFilter filterWithName:@"CIGaussianBlur"];
 [filter1 setValue:inputImage forKey:kCIInputImageKey];
 CIImage *result = [filter1 valueForKey:kCIOutputImageKey];
 CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
 UIImage *returnImage = [UIImage imageWithCGImage:cgImage]; // returnImage is your blur image
 CGImageRelease(cgImage);