【问题标题】:Having trouble creating UIImage from CIImage in iOS5在 iOS5 中从 CIImage 创建 UIImage 时遇到问题
【发布时间】:2011-12-08 23:04:02
【问题描述】:

我正在使用 AVFoundation 框架。在我的示例缓冲区委托中,我有以下代码:

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
     CVPixelBufferRef pb = CMSampleBufferGetImageBuffer(sampleBuffer);
     CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pb];
     self.imageView.image = [UIImage imageWithCIImage:ciImage];
}

我可以使用 CIImage 来运行人脸检测器等,但它没有显示在 UIImageView 中……imageView 仍然是白色的。关于这个问题的任何想法? 我正在使用以下内容来设置我的会话:

    self.session = [[AVCaptureSession alloc] init];
self.session.sessionPreset = AVCaptureSessionPreset640x480;
self.videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil]; 
self.frameOutput = [[AVCaptureVideoDataOutput alloc] init];
self.frameOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];

【问题讨论】:

    标签: iphone ios avfoundation ios5 core-image


    【解决方案1】:

    这可能会有所帮助。这段代码我遇到了同样的问题(图像没有被绘制到屏幕上):

    CIImage *image = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"mushroom.jpg"]];
    
    theImageView.image = [UIImage imageWithCIImage:image];
    

    但是,在将代码更改为此之后,它现在可以正常工作了:

    CIImage *image = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"mushroom.jpg"]];
    
    CIContext *context = [CIContext contextWithOptions:nil];
    
    theImageView.image = [UIImage imageWithCGImage:[context createCGImage:image fromRect:image.extent]];
    

    要了解有关 CIContext 的更多信息,请查看此处: http://developer.apple.com/library/ios/#DOCUMENTATION/GraphicsImaging/Reference/QuartzCoreFramework/Classes/CIContext_Class/Reference/Reference.html#//apple_ref/occ/cl/CIContext

    【讨论】:

    • 谢谢——这确实让我可以画出图像。但是它会泄漏内存,导致应用程序在运行几秒钟后崩溃。
    • 是的,我还没有真正深入研究它。我确定您必须在某个时候释放上下文或 CGImageRef。另外我确信上面的方法不是使用 CIImage 视图的实用方法。我计划明天观看“在 iOS 和 Mac OS X 上使用 Core Image”视频(在 Apple Developer 网站上,WWDC 2011 会议视频)并深入了解,如果我学到任何有用的东西,我会放弃.
    • 我建议您也观看此视频和/或查看演示幻灯片,因为它解释了如何使用 Core Image,并详细介绍了上下文以及何时使用 CPU/GPU 等等
    • 我还发现这很有用:OpenGL ES Programming Guide for iOS developer.apple.com/library/ios/#documentation/3DDrawing/…
    • @akaru 下面是 UIImage 参考中关于其 CGImage 属性的说明:“如果 UIImage 对象是使用 CIImage 对象初始化的,则该属性的值为 NULL”。看起来 UIImageView 试图在其实现中访问 UIImage 的 CGImage。 UIImagePNGRepresentation 函数似乎也是如此 - 如果 UIImage 不是基于 CGImage 的,它返回 nil。但是 +[CIContext createCGImage:] 只是按照它说的做 - 创建 CGImage,所以它可以工作
    【解决方案2】:

    这是我让它工作的一种方法:

    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef ref = [context createCGImage:result fromRect:ciImage.extent];
    self.imgView.image = [UIImage imageWithCGImage:ref scale:1.0 orientation:UIImageOrientationRight];
    CGImageRelease(ref);
    

    注意:多次创建上下文真的很糟糕,实际上会导致内存泄漏。您应该只在类实例中创建一次上下文作为属性并重用它!

    【讨论】:

      【解决方案3】:
      CIImage *ciImage = [UIImage imageNamed:@"imageName.png"].CIImage;
      UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage];
      

      【讨论】:

      • 对我不起作用。我创建 CIImage 的方式与此处显示的方式不同。
      【解决方案4】:

      这是我在项目中使用的完整代码示例。

      - (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

      【讨论】:

      • 太棒了,谢谢!!!我想知道为什么 Apple 提供 [UIImage imageWithCIImage] 如果它很少起作用。谢谢苹果。
      • 我遇到了一些非常微妙的颜色失真和 alpha 混合问题,通过设置颜色空间 context = [CIContext contextWithOptions:@{kCIContextWorkingColorSpace : [NSNull null]}];
      • 感谢@user1055568,这是我们需要的关键信息
      猜你喜欢
      • 2012-02-24
      • 2017-04-14
      • 1970-01-01
      • 2013-01-15
      • 2021-05-30
      • 2014-12-05
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      相关资源
      最近更新 更多