简体   繁体   English

在iOS5中从CIImage创建UIImage时遇到问题

[英]Having trouble creating UIImage from CIImage in iOS5

I'm using the AVFoundation framework. 我正在使用AVFoundation框架。 In my sample buffer delegate I have the following code: 在我的示例缓冲区委托中,我有以下代码:

-(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];
}

I am able to use the CIImage to run the face detector etc. but it does not show up in the UIImageView ... the imageView remains white. 我可以使用CIImage来运行面部检测器等但它没有出现在UIImageView中...... imageView仍然是白色的。 Any ideas as to the problem? 关于这个问题的任何想法? I am using the following to setup my session: 我使用以下设置我的会话:

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

This might help. 这可能有所帮助。 I was having the same issue (image not being drawn to screen) with this code: 使用此代码我遇到了同样的问题(图像没有被绘制到屏幕上):

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

theImageView.image = [UIImage imageWithCIImage:image];

However, after changing the code to this, it now works correctly: 但是,在将代码更改为此后,它现在可以正常工作:

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

CIContext *context = [CIContext contextWithOptions:nil];

theImageView.image = [UIImage imageWithCGImage:[context createCGImage:image fromRect:image.extent]];

To read more about CIContext take a look here: http://developer.apple.com/library/ios/#DOCUMENTATION/GraphicsImaging/Reference/QuartzCoreFramework/Classes/CIContext_Class/Reference/Reference.html#//apple_ref/occ/cl/CIContext 要了解有关CIContext的更多信息,请访问: http//developer.apple.com/library/ios/#DOCUMENTATION/GraphicsImaging/Reference/QuartzCoreFramework/Classes/CIContext_Class/Reference/Reference.html#//apple_ref/occ/cl / CIContext

Here is one way that I got it to work: 这是我让它工作的一种方式:

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

Note: Creating the context multiple times is really bad and actually causes a memory leak. 注意:多次创建上下文非常糟糕,实际上会导致内存泄漏。 You should just create the context once as a property in your class instance and reuse it! 您应该只在类实例中创建一次属性作为属性并重用它!

CIImage *ciImage = [UIImage imageNamed:@"imageName.png"].CIImage;
UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage];

Here is a complete code sample that I've used in my projects. 这是我在项目中使用的完整代码示例。

- (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 is basically a recipe for an image. CIImage基本上是图像的配方。 You will run into problems if you try to directly convert from CIImage to UIImage . 如果您尝试直接从CIImage转换为UIImage您将遇到问题。 For example, calling 例如,打电话

NSData * UIImageJPEGRepresentation (
   UIImage *image,
   CGFloat compressionQuality
);

will return nil . 将返回nil

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

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