简体   繁体   English

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

[英]UIImage from CIImage - Data length is zero?

I'm using an AVCaptureVideoDataOutput along with its delegate method to manipulate video frames. 我正在使用AVCaptureVideoDataOutput及其委托方法来处理视频帧。 In the delegate method, I am using the sampleBuffer to create a CIImage , and from here I crop the CIImage , convert it to a UIImage and display it. 在委托方法中,我使用sampleBuffer创建一个CIImage ,从这里我裁剪CIImage ,将其转换为UIImage并显示它。 Unfortunately, I need to determine the file-size of this new UIImage , but it's returning 0 . 不幸的是,我需要确定这个新UIImage的文件大小,但它返回0 The code works, the image is cropped beautifully, everything. 代码工作,图像裁剪精美,一切。 I just don't see why it has no data! 我只是不明白为什么它没有数据!

Why might this be? 为什么会这样? Relevant code follows: 相关代码如下:

//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];
});

I had the same Problem, but with simple filtered images. 我有同样的问题,但使用简单的过滤图像。

I stumbled upon this and it solved the issue. 我偶然发现了 ,它解决了这个问题。 After this, I was able to save my image. 在此之后,我能够保存我的图像。

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

But I only needed this two lines in the "ImageContext" 但我在“ImageContext”中只需要这两行

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

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