Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Quartz2d: import image

surlacsurlac Posts: 362New Users @ @
I'm reading Quart2d tutorial, and stumbled upon importing image via CGDataProviderCreateWithURL. Everything is working if I supply full path to image file.
How would I make it work relative to project directory? (Or the problem is that it can not be loaded from application bundle?).

AppDelegate.m
@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // works if I provide full path, otherwise - can not find an image 
    const UInt8 *inFile = (const UInt8 *)"/FULL_PATH_TO_PROJECT/image.png";
    FSRef whichFile;
    FSPathMakeRef(inFile, &whichFile, NULL);
    
    CFURLRef fileURL = CFURLCreateFromFSRef(NULL, &whichFile);
    CGDataProviderRef dataProvider = CGDataProviderCreateWithURL(fileURL);
    CFRelease(fileURL);
    fileURL = NULL;
    
    CGImageSourceRef imageSource = CGImageSourceCreateWithDataProvider(dataProvider, NULL);
    CGDataProviderRelease(dataProvider);
    dataProvider = NULL;
    
    CGImageRef image = CGImageSourceCreateImageAtIndex(imageSource, 0, 0);
    CFRelease(imageSource);
    imageSource = NULL;
    
    self.imageWell.image = [[NSImage alloc] initWithCGImage:image size:CGSizeMake(100, 100)];
}
Sign In or Register to comment.