来自CIImage的UIImage - 数据长度为零?

时间:2013-02-25 23:38:44

标签: objective-c cocoa-touch uiimage core-image

我正在使用AVCaptureVideoDataOutput及其委托方法来操纵视频帧。在委托方法中,我使用sampleBuffer创建CIImage,然后从此处裁剪CIImage,将其转换为UIImage并显示它。不幸的是,我需要确定这个新UIImage的文件大小,但它正在返回0。代码工作,图像裁剪精美,一切。我只是不明白为什么它没有数据!

为什么会这样?相关代码如下:

//In delegate method, given sampleBuffer...
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault,
                                  sampleBuffer, kCMAttachmentMode_ShouldPropagate);
CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer 
                                                  options:(NSDictionary *)attachments];

...


dispatch_async(dispatch_get_main_queue(), ^(void) {
    CGRect rect = [self drawFaceBoxesForFeatures:features forVideoBox:clap
                                                 orientation:curDeviceOrientation];

    CIImage *cropped = [ciImage imageByCroppingToRect:rect];
    UIImage *image = [[UIImage alloc] initWithCIImage:cropped];

    NSData *data = UIImageJPEGRepresentation(image, 1);
    NSLog(@"Image size is %d", data.length); //returns 0???

    [imageView setImage:image];

    [image release];
});

1 个答案:

答案 0 :(得分:2)

我有同样的问题,但是使用简单的过滤图像。

我偶然发现this并解决了这个问题。在此之后,我能够保存我的图像。

CGSize size = self.originalImage.size;
CGRect rect;
rect.origin = CGPointZero;
rect.size   = size;

UIGraphicsBeginImageContext(size);
[[UIImage imageWithCIImage:self.filteredImage] drawInRect:rect];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData * jpegData = UIImageJPEGRepresentation(image, 1.0);

但我只需要在" ImageContext"

中使用这两行