简体   繁体   English

无法从CIImage获取图像数据

[英]Unable to get Image Data from CIImage

I am using CIImage and CIFilter to doing image filtration. 我正在使用CIImageCIFilter进行图像过滤。 After that, I am trying to get image data by UIImageJPEGRepresentation , but I am getting null data. 在那之后,我试图通过UIImageJPEGRepresentation获取图像数据,但是我正在获取空数据。

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);

I am getting null data when I console imageData. 当我管理imageData时,我得到的是空数据。 Please check my above code, and let me know where I am doing wrong. 请检查我上面的代码,并让我知道我在哪里做错了。

Your problem is here you are passing CIImage 您的问题是您正在传递CIImage

UIImage *filteredImage = [UIImage imageWithCIImage:outputImage];

But you need to pass CGImageRef while creating UIImage write this 但是您需要在创建UIImage传递CGImageRef来编写此代码

 UIImage *filteredImage = [[UIImage alloc] initWithCGImage:cgimg];

I hope it will help you 希望对您有帮助

// create our blurred image try this hope its work for you. //创建我们模糊的图像,尝试这一希望它对您有用。

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM