我正试图从屏幕上显示的ImageView获取CIImage
。
UIImage *image = myImageView.image;
将图片UIImage
转换为CIImage
。
CIImage *cImage = [....];
怎么做?
答案 0 :(得分:117)
CIImage *ciImage = [UIImage imageNamed:@"test.png"].CIImage;
UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage];
要修复myUIImage.CIImage
返回nil
[UIImageView image]
之类的情况,您可以改为[CIImage imageWithCGImage:myUIImage.CGImage]
- Dylan Hand
Swift版本:
let ciImage = UIImage(named: "test.png")!.ciImage
let uiImage = UIImage(cgImage: ciImage)
答案 1 :(得分:31)
虽然Changxing Wang的回答是有效的,但Apple说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];
Apple也使用以下内容,但这对我不起作用:
[UIImage imageWithCIImage:_filteredImage]
我的个人实施改编自this post对我有用:
// 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]];
答案 2 :(得分:19)
这是最兼容的方式:
UIImage* u =[UIImage imageNamed:@"image.png"];
CIImage* ciimage = [[CIImage alloc] initWithCGImage:u.CGImage];
现在使用ciimage ...
答案 3 :(得分:13)
这是我在项目中使用的完整代码示例。
- (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
。
答案 4 :(得分:11)
func toCIImage(image: UIImage) -> CIImage {
return image.CIImage ?? CIImage(CGImage: image.CGImage!)
}
func toUIImage(image: CIImage) -> UIImage {
return UIImage(CIImage: image)
}
答案 5 :(得分:4)
changx答案的快速示例:
guard let ciimage = CIImage(image: uiimage) else { return }
let image = UIImage(CIImage: ciimage)
答案 6 :(得分:3)
Swift 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
答案 7 :(得分:0)
当将UIImage转换为CIImage时,我有两个独立的案例,有时甚至是零,有时不是。下面的代码解决了它:
CIImage *ciImage;
ciImage=[[CIImage alloc] initWithImage:imageToConvert];
if (ciImage==nil)
ciImage=imageToConvert.CIImage;