Professional Web Applications Themes

Drawing an Image with CoreGraphics - Mac Programming

I am developing a Cocoa Game and I want to draw an image (PNG would be acceptable, TIFF better) into a View. I am using a subclass of NSView. The application either doesn't draw the image or crashes. { CGContextRef graphicsContext = [[NSGraphicsContext currentContext] graphicsPort]; NSString *imagePath = [NSString stringWithString:"/Users/developer/Projects/Artillery/artillery.png"]; NSData *imageData = [NSData dataWithContentsOfFile:imagePath]; //Loads the image. CGDataProviderRef source = (CGDataProviderRef)imageData; CGImageRef sprite; sprite = CGImageCreateWithPNGDataProvider ( source, NULL, NO, kCGRenderingIntentDefault ); // Anzeigen des Bildes. CGContextDrawImage ( graphicsContext, convertToCGRect(rect), sprite ); CGImageRelease ( //Is this required? sprite ); } Does anyone know what I made wrong? How can ...

  1. #1

    Default Drawing an Image with CoreGraphics

    I am developing a Cocoa Game and I want to draw an image (PNG would be
    acceptable, TIFF better) into a View. I am using a subclass of NSView.
    The application either doesn't draw the image or crashes.

    {
    CGContextRef graphicsContext = [[NSGraphicsContext currentContext]
    graphicsPort]; NSString *imagePath = [NSString
    stringWithString:"/Users/developer/Projects/Artillery/artillery.png"];
    NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
    //Loads the image.
    CGDataProviderRef source = (CGDataProviderRef)imageData;
    CGImageRef sprite;
    sprite = CGImageCreateWithPNGDataProvider (
    source,
    NULL,
    NO,
    kCGRenderingIntentDefault
    );
    // Anzeigen des Bildes.
    CGContextDrawImage (
    graphicsContext,
    convertToCGRect(rect),
    sprite
    );
    CGImageRelease ( //Is this required?
    sprite
    );
    }

    Does anyone know what I made wrong? How can I use a (packed/unpacked?)
    TIFF?

    Thanks in advance
    ro86 Guest

  2. #2

    Default Re: Drawing an Image with CoreGraphics

    In article <google.com>,
    com (ro86) wrote:
     

    This isn't your problem, but there's no need for the call to
    stringWithString. Just

    imagePath = "<whatever>";

    should do.
     

    Now, I don't know what exactly is toll-free bridged with cocoa and what
    is not, but I don't think you can just cast an NSData to a
    CGDataProvider. In this case you should create your data provider with

    CGDataProviderCreateWithURL(url_pointing_to_your_f ile);

    I think that NSURL and CFURL are toll-free bridged, so you can use a
    NSURL* as the argument to this function.
     

    Yes, and you will need to release the data provider as well.

    All of this may be more work than you need to do; are you sure NSImage
    won't do what you want?

    -Chad
    Chad Guest

  3. #3

    Default Re: Drawing an Image with CoreGraphics

    Chad J McQuinn <andthistoo.insightbb.com> wrote in message news:<comcast.net>... 
    >
    > This isn't your problem, but there's no need for the call to
    > stringWithString. Just
    >
    > imagePath = "<whatever>";
    >
    > should do.

    >
    > Now, I don't know what exactly is toll-free bridged with cocoa and what
    > is not, but I don't think you can just cast an NSData to a
    > CGDataProvider. In this case you should create your data provider with
    >
    > CGDataProviderCreateWithURL(url_pointing_to_your_f ile);
    >
    > I think that NSURL and CFURL are toll-free bridged, so you can use a
    > NSURL* as the argument to this function.

    >
    > Yes, and you will need to release the data provider as well.
    >
    > All of this may be more work than you need to do; are you sure NSImage
    > won't do what you want?
    >
    > -Chad[/ref]

    Thanks!

    I have decided to use NSImage for now but I will defiantly use the
    CG-functions in future. CGDataProvider is indeed not toll-free bridged
    to NSData, NSData is only toll-free bridged to CFData. NSURL and CFURL
    are toll-free bridged.

    Robert
    ro86 Guest

Similar Threads

  1. CloneStam not Drawing image
    By Unregistered in forum Brainstorming Area
    Replies: 0
    Last Post: August 15th, 10:10 PM
  2. Replies: 1
    Last Post: September 10th, 10:39 PM
  3. Replies: 0
    Last Post: September 8th, 10:54 PM
  4. Replies: 0
    Last Post: September 8th, 09:33 PM
  5. #25438 [NEW]: GD Fails drawing more than one image with TTF at one time
    By sven at weih dot de in forum PHP Development
    Replies: 0
    Last Post: September 8th, 08:51 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139