cacao icon indicating copy to clipboard operation
cacao copied to clipboard

Capability to resize Images

Open LeoBorai opened this issue 9 months ago • 3 comments

It would be nice to have support to set a fixed height and width for an image.

Im using cacao::image::Image instance with Image::with_data to set an image from bytes into a view, but I don't seem to find a way to set image dimensions.

I have found the following snippet on StackOverflow on how could it be done:

- (NSImage *)imageResize:(NSImage*)anImage newSize:(NSSize)newSize {
    NSImage *sourceImage = anImage;
    [sourceImage setScalesWhenResized:YES];
    
    // Report an error if the source isn't a valid image
    if (![sourceImage isValid]){
        NSLog(@"Invalid Image");
    } else {
        NSImage *smallImage = [[NSImage alloc] initWithSize: newSize];
        [smallImage lockFocus];
        [sourceImage setSize: newSize];
        [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
        [sourceImage drawAtPoint:NSZeroPoint fromRect:CGRectMake(0, 0, newSize.width, newSize.height) operation:NSCompositingOperationCopy fraction:1.0];
        [smallImage unlockFocus];
        return smallImage;
    }
    return nil;
}

https://stackoverflow.com/questions/11949250/how-to-resize-nsimage

But given that Im not very familiar with Objective-C Im not pretty sure if there could be a simpler approach.

Thanks in advance!

LeoBorai avatar Jun 03 '24 23:06 LeoBorai