0

Hello I have the following function to save a jpg image with a scale factor

- (UIImage*) pixelBufferToUIImage:(CVPixelBufferRef) pixelBuffer
{
    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];

    CIContext *temporaryContext = [CIContext contextWithOptions:nil];
    CGImageRef videoImage = [temporaryContext
                             createCGImage:ciImage
                             fromRect:CGRectMake(0, 0,
                                                 CVPixelBufferGetWidth(pixelBuffer),
                                                 CVPixelBufferGetHeight(pixelBuffer))];
    UIImage *uiImage = [UIImage imageWithCGImage:videoImage scale:(1/scale_factor) orientation:UIImageOrientationUp];
    CGImageRelease(videoImage);
    return uiImage;
}

I call this method like so,

NSData* image = UIImageJPEGRepresentation([self pixelBufferToUIImage:frame.capturedImage], 0.8);
[image writeToFile:[self getFilePathWith:folderName and:@"image.jpg"] atomically:NO];

If I call the save with a scale_factor = 1.0f which is the original resolution I get a 1280x720 image

However, I want to half the image size. I tried setting scale_factor = 0.5f and 2.0f Both did not work and I get the jpg's size as 1280x720

Could anyone point out my mistake here?

Pavan K
  • 4,085
  • 8
  • 41
  • 72

1 Answers1

0

The documentation says that the scale will change what the size property reports. It does not change the image's pixel data. The internal representation of the UIImage still has the same dimensions.

scale
The scale factor to use when interpreting the image data. Specifying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.

To scale your image you need to do the scaling yourself.

Have a look here how to do it

So you could do it like this. Put the method in the link in your class, then add the scaling call before converting your image to JPEG.

CGFloat newScale = 0.5;
UIImage *uiImage = [self pixelBufferToUIImage:frame.capturedImage];
uiImage = [YourClass imageWithImage:uiImage scaledToSize:CGSizeMake(uiImage.size.width * newScale, uiImage.size.height * newScale)];
NSData* image = UIImageJPEGRepresentation(uiImage, 0.8);
[image writeToFile:[self getFilePathWith:folderName and:@"image.jpg"] atomically:NO];
LGP
  • 4,135
  • 1
  • 22
  • 34