我试图CIImage
从屏幕上显示的ImageView获取。
UIImage *image = myImageView.image;
将图像转换UIImage
为CIImage
。
CIImage *cImage = [....];
这该怎么做?
Answers:
CIImage *ciImage = [UIImage imageNamed:@"test.png"].CIImage;
UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage];
要解决类似这样的myUIImage.CIImage
回报问题,您可以改用– Dylan Handnil
[UIImageView image]
[CIImage imageWithCGImage:myUIImage.CGImage]
迅捷版:
let ciImage = UIImage(named: "test.png")!.ciImage
let uiImage = UIImage(ciImage: ciImage)
要解决类似myUIImage.ciImage
返回的情况,nil
您可以这样做CIImage(cgImage: myUIImage!.cgImage!)
。
UIImage
初始化为CGImageRef
CIImage
将是nil
,它将无法正常工作。
UIImage
与创建...WithCIImage:
确实有一些问题,调整大小,因为大多数的框架有UIImage
需要的CGImage
。因此,您需要先使用createCGImage:fromRect:
或类似的东西渲染它。
myUIImage.CIImage
返回的情况nil
,您可以改为执行[CIImage imageWithCGImage:myUIImage.CGImage]
。
尽管王长兴的答案实际上是正确的,但苹果公司表示UIImage.CIImage不会一直有效。具体来说,来自PocketCoreImage:
// Create a CIImage from the _inputImage. While UIImage has a property returning
// a CIImage representation of it, there are cases where it will not work. This is the
// most compatible route.
_filteredImage = [[CIImage alloc] initWithCGImage:_inputImage.CGImage options:nil];
苹果还使用以下内容,但对我不起作用:
[UIImage imageWithCIImage:_filteredImage]
从这篇文章改编的我的个人实现确实对我有用:
// result is a CIImage, output of my filtering.
// returnImage is (obviously) a UIImage that I'm going to return.
CIContext *context = [CIContext contextWithOptions:nil];
UIImage *returnImage =
[UIImage imageWithCGImage:[context createCGImage:result fromRect:result.extent]];
这是最兼容的方式:
UIImage* u =[UIImage imageNamed:@"image.png"];
CIImage* ciimage = [[CIImage alloc] initWithCGImage:u.CGImage];
现在使用ciimage ...
和Swift版本:
let i1 = UIImage(named: "testImage")
if let cgi1 = i1?.cgImage {
let ci = CIImage(cgImage: cgi1)
//use ci
}
(修复了if let语句中的错字)
这是我在项目中使用的完整代码示例。
- (UIImage *)makeUIImageFromCIImage:(CIImage *)ciImage {
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:ciImage fromRect:[ciImage extent]];
UIImage* uiImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return uiImage;
}
CIImage
基本上是图像的配方。如果您尝试直接从转换为CIImage
,则会遇到问题UIImage
。例如,打电话
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
会回来的nil
。
changx答案的Swift示例:
guard let ciimage = CIImage(image: uiimage) else { return }
let image = UIImage(CIImage: ciimage)
nil
如果不是从ciimage构建的,则将返回
迅捷3/4
UIImage> CIImage
let ciimage = CIImage(image: image)
https://developer.apple.com/documentation/uikit/uiimage/1624090-init
CIImage> UIImage
let image = UIImage(ciImage: ciimage)
https://developer.apple.com/documentation/coreimage/ciimage/1624119-init
将UIImage转换为CIImage时,我有2种单独的情况,有时有时为零,有时则为零。下面的代码解决了它:
CIImage *ciImage;
ciImage=[[CIImage alloc] initWithImage:imageToConvert];
if (ciImage==nil)
ciImage=imageToConvert.CIImage;
[UIImage imageWithCIImage:ciImage]
等效于[[[UIImage alloc] initWithCIImage:ciImage] autorelease]