如何将UIImage转换为CIImage,反之亦然

Hel*_*miB 64 cocoa-touch core-graphics core-image uiimage ios5

我正试图CIImage从屏幕上显示的ImageView 获取.

UIImage *image = myImageView.image;
Run Code Online (Sandbox Code Playgroud)

将图像转换UIImageCIImage.

CIImage *cImage = [....];
Run Code Online (Sandbox Code Playgroud)

这该怎么做?

cha*_*ngx 123

CIImage *ciImage = [UIImage imageNamed:@"test.png"].CIImage;
UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage];
Run Code Online (Sandbox Code Playgroud)

要解决其中的情况下myUIImage.CIImage返回nil喜欢[UIImageView image],你可以改为做[CIImage imageWithCGImage:myUIImage.CGImage]-迪伦手

Swift版本:

let ciImage = UIImage(named: "test.png")!.ciImage
let uiImage = UIImage(ciImage: ciImage)
Run Code Online (Sandbox Code Playgroud)

  • 小心点 如果'UIImage`用`CGImageRef``初始化``CIImage`将是'nil`,这将不起作用. (46认同)
  • 是的,这里是`[UIImage imageWithCIImage:ciImage]` (6认同)
  • 警告不适用于[UIImageView图像] (5认同)
  • 使用`... WithCIImage:`创建的`UIImage`在调整大小方面确实存在一些问题,因为大多数带有`UIImage`的框架需要一个`CGImage`.所以你需要首先使用`createCGImage:fromRect:`或类似的东西来渲染它. (4认同)
  • 对于它的价值,总会有一个对alloc的调用...``[UIImage imageWithCIImage:ciImage]`相当于`[[[UIImage alloc] initWithCIImage:ciImage] autorelease]` (3认同)
  • 要修复 `myUIImage.CIImage` 返回 `nil` 的情况,您可以改为执行 `[CIImage imageWithCGImage:myUIImage.CGImage]`。 (2认同)

小智 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];
Run Code Online (Sandbox Code Playgroud)

Apple也使用以下内容,但这对我不起作用:

[UIImage imageWithCIImage:_filteredImage] 
Run Code Online (Sandbox Code Playgroud)

我的个人实现,改编自这篇文章对我有用:

// 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]];
Run Code Online (Sandbox Code Playgroud)


Jur*_*tas 21

这是最兼容的方式:

UIImage* u =[UIImage imageNamed:@"image.png"];
CIImage* ciimage = [[CIImage alloc] initWithCGImage:u.CGImage];
Run Code Online (Sandbox Code Playgroud)

现在使用ciimage ...

  • 要从 UIImage 创建 CIImage,使用 initWithCGImage 是唯一有效的解决方案。做 'uiimagei.CIImage 不起作用,它总是为零。 (2认同)

JVi*_*lla 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;
}
Run Code Online (Sandbox Code Playgroud)

CIImage基本上是图像的配方.如果您尝试直接转换为CIImage,则会遇到问题UIImage.例如,打电话

NSData * UIImageJPEGRepresentation (
   UIImage *image,
   CGFloat compressionQuality
);
Run Code Online (Sandbox Code Playgroud)

会回来的nil.


Eta*_*tan 11

func toCIImage(image: UIImage) -> CIImage {
    return image.CIImage ?? CIImage(CGImage: image.CGImage!)
}

func toUIImage(image: CIImage) -> UIImage {
    return UIImage(CIImage: image)
}
Run Code Online (Sandbox Code Playgroud)


use*_*924 7

斯威夫特 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


Dan*_*orm 5

changx答案的Swift示例:

guard let ciimage = CIImage(image: uiimage) else { return }
let image = UIImage(CIImage: ciimage)
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

50987 次

最近记录:

6 年,5 月 前