0

我正在使用AVCaptureVideoDataOutput它的委托方法来操作视频帧。在委托方法中,我使用sampleBuffer来创建 a CIImage,然后从这里裁剪CIImage,将其转换为 aUIImage并显示它。不幸的是,我需要确定这个 new 的文件大小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];
});
4

1 回答 1

2

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

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

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”中的这两行

于 2014-11-18T14:45:51.627 回答