Professional Web Applications Themes

Problem in outputting PNG image - Mac Programming

Hi, I try to export a PNG file of the content of my NSImageView and get an error I do not understand... talking about NSCFString. Thank you for any help, Loic ---------------------------------------------------------------- Here is the code: -------------- - (IBAction)exportDoent: (id)sender { int runResult; NSSavePanel *theSavePanel; NSData *theData; NSBitmapImageRep *theBitmap = [NSBitmapImageRep alloc]; theSavePanel = [NSSavePanel savePanel]; [theSavePanel setAccessoryView: exportAccessoryView]; [theSavePanel setTitle:"Export"]; runResult = [theSavePanel runModalForDirectory:NSHomeDirectory()file:"aeff. png"]; // if successful, save file under designated name if (runResult == NSOKButton) { NSString *aFile = [theSavePanel filename]; switch(exportFormat) { case FFTPNG_PNGCOW: [theSavePanel setRequiredFileType:"png"]; NSLog(aFile); NSLog(" 1"); theData = [[activeImageView image] TIFFRepresentation]; [theBitmap initWithData:theData]; ...

Sponsored Links
  1. #1

    Default Problem in outputting PNG image

    Hi,

    I try to export a PNG file of the content of my NSImageView and get an
    error I do not understand... talking about NSCFString.

    Thank you for any help,

    Loic

    ----------------------------------------------------------------
    Here is the code:
    --------------

    - (IBAction)exportDoent: (id)sender {
    int runResult;
    NSSavePanel *theSavePanel;
    NSData *theData;
    NSBitmapImageRep *theBitmap = [NSBitmapImageRep alloc];

    theSavePanel = [NSSavePanel savePanel];
    [theSavePanel setAccessoryView: exportAccessoryView];
    [theSavePanel setTitle:"Export"];

    runResult = [theSavePanel
    runModalForDirectory:NSHomeDirectory()file:"aeff. png"];

    // if successful, save file under designated name
    if (runResult == NSOKButton) {
    NSString *aFile = [theSavePanel filename];

    switch(exportFormat) {
    case FFTPNG_PNGCOW:
    [theSavePanel setRequiredFileType:"png"];
    NSLog(aFile);

    NSLog(" 1");
    theData = [[activeImageView image] TIFFRepresentation];
    [theBitmap initWithData:theData];
    NSLog(" 4");
    [[theBitmap representationUsingType:NSPNGFileType properties:nil]
    writeToFile:aFile atomically:YES];
    NSLog(" 5");
    break;

    case FFTPNG_TXTMAT:
    [... code ...]
    }
    }
    return;
    }

    ------------------------------------------------------------------
    in Log window
    ------------

    2003-10-16 15:30:06.674 pngtf[1177] /Users/loic/aeff.png
    2003-10-16 15:30:06.674 pngtf[1177] 1
    2003-10-16 15:30:06.686 pngtf[1177] 4
    2003-10-16 15:30:06.742 pngtf[1177] *** -[NSCFString
    representationUsingType:properties:]: selector not recognized
    2003-10-16 15:30:06.752 pngtf[1177] *** -[NSCFString
    representationUsingType:properties:]: selector not recognized

    The program does not crash and the method exits here (" 5" is not
    printed).
    Sponsored Links
    Loic Guest

  2. #2

    Default Re: Problem in outputting PNG image

    In article <google.com>,
    fr (Loic BERTRAND) wrote:
     

    An error such as what you saw usually means that the object you wanted
    was deallocated, and another object (in this case an NSString) was
    allocated at the same location in memory.
     

    This line is wrong in a small way and a big way.

    The small way: normally you do alloc and init all at once. The "right"
    way to do things would be to simply declare (but not initialize)
    theBitmap at the top of your function, and then write this:

    theBitmap = [[NSBitmapImageRep alloc] initWithData:theData];

    Alternatively, you can declare it at this point as well if your C
    compiler lets you do that.

    The big way: you are *required* to assign your variable to the return
    value of any init... message. So the line you wrote must be written like
    this:

    theBitmap = [theBitmap initWithData:theData];

    The reason for this is that init... messages can, and often do, return a
    different object from the one you send it to. Very often, Cocoa classes
    return a temporary placeholder object from [Class alloc], and then
    deallocate that temporary object and allocate a real object from a
    dynamically-chosen class when you do init. I assume this is what is
    happening in this case. So your original object goes away, but you don't
    reassign theBitmap so you lose it, and thus your crash.

    As I said above, though, this is best fixed by always doing [[Class
    alloc] init...] and never separating the two stages.
    Michael Guest

  3. #3

    Default Re: Problem in outputting PNG image

    Thank you for your very precise answer.

    Loc
    Loic Guest

Similar Threads

  1. Problems outputting to PDF and PS
    By Lyle McDonald in forum Adobe Acrobat Macintosh
    Replies: 1
    Last Post: January 24th, 09:37 PM
  2. Outputting an animation 101
    By Carlton_Chin@adobeforums.com in forum Adobe Photoshop Mac CS, CS2 & CS3
    Replies: 3
    Last Post: April 16th, 06:02 PM
  3. [PHP] XX outputting X signs
    By George Pitcher in forum PHP Development
    Replies: 1
    Last Post: July 22nd, 03:08 PM
  4. £ outputting signs
    By Bob pilly in forum PHP Development
    Replies: 1
    Last Post: July 22nd, 01:59 PM
  5. outputting XML from PHP
    By Aarron in forum PHP Development
    Replies: 1
    Last Post: July 21st, 03:08 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