Saturday, March 4, 2006

rendering images in Cocoa

I was having a problem getting image files to render in Cocoa, using Objective-C.  Here is the code that works for me:

    NSString *shortPath = @"~/Desktop/DCP_4146.JPG";
    NSString *absolutePath = [shortPath stringByExpandingTildeInPath];
    NSImage *image = [[NSImage alloc] initWithContentsOfFile: absolutePath];
    NSSize imgSize;
    NSRect dstRect;
    NSRect imgRect;
    // how much room do we have to play in?
    NSRect bounds = [self bounds];
    imgSize = [image size];

    // Should really be a while loop to scale down truly huge pictures
    dstRect.size.width = imgSize.width;
    dstRect.size.height = imgSize.height;
    imgRect = NSMakeRect(0, 0, imgSize.width, imgSize.height);

    [image drawInRect:dstRect fromRect:imgRect operation:NSCompositeSourceOver fraction:1.0];

Technorati Tags: ,

No comments:

Post a Comment

Revolutionizing Air Quality Monitoring: How I Upgraded Our System with Mila Integration for Smart Home Automation

In this blog post , I explained how I set up an air quality monitoring system for our neighborhood. With this setup, we can keep an eye on t...