anon_dev1234
anon_dev1234

Reputation: 2153

UIImage from CIImage - Data length is zero?

I'm using an AVCaptureVideoDataOutput along with its delegate method to manipulate video frames. 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. Unfortunately, I need to determine the file-size of this new UIImage, but it's returning 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];
});

Upvotes: 0

Views: 1244

Answers (1)

due
due

Reputation: 21

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"

Upvotes: 2

Related Questions