Image Batch Processing

  • HI, I've been trying to do a batch converter for images. The problem
    is the memory is not being released properly. When the for ends the
    memory is ok, but during the for it continues increasing. I have a
    function with a for loop:
          for(count=0;count<cant;count++)
          {
                  path=[datos objectAtIndex:count];
                  save_name=[NSString stringWithFormat:
                                  @"image_%04d.jpg",count+1];
                  output=[[batch_outdir stringValue]
    stringByAppendingPathComponent:save_name];
                  [self convert:path path_out:output format:NSJPEGFileType quality:
    0.3];
          }

    and then the conversion function

    -(void)convert:(NSString*)path_in path_out:(NSString*)path_out format:
    (NSBitmapImageFileType)type quality:(float)q
    {
          NSDictionary *d=[NSDictionary dictionaryWithObject:
                                                  [NSNumber numberWithFloat:q]
                                                  forKey:NSImageCompressionFactor];
          NSImage *tmp_img;
          NSBitmapImageRep *b;
          NSData *data;

          tmp_img=[[NSImage alloc] initWithContentsOfFile:path_in];
          b=[NSBitmapImageRep imageRepWithData:[tmp_img TIFFRepresentation]];
          [tmp_img release];

          data=[b representationUsingType:tipo properties:d];
          [data writeToFile:path_out atomically:TRUE];
    }

    --
    http://zon7blog.blogspot.com/
    And again we fall.
  • On Fri, September 21, 2007 11:19 am, Jere Gmail said:
    > HI, I've been trying to do a batch converter for images. The problem
    > is the memory is not being released properly. When the for ends the
    > memory is ok, but during the for it continues increasing. I have a
    > function with a for loop:

    Basically you've got autoreleased objects that are hanging around until
    the loop is completed.  You may want to create your own autorelease pool
    at the beginning of the loop and release it at the end.  (Or once every
    few cycles, depending on how much the memory is growing and what you can
    stand.)

    Daniel T. Staal

    ---------------------------------------------------------------
    This email copyright the author.  Unless otherwise noted, you
    are expressly allowed to retransmit, quote, or otherwise use
    the contents for non-commercial purposes.  This copyright will
    expire 5 years after the author's death, or in 30 years,
    whichever is longer, unless such a period is in excess of
    local copyright law.
    ---------------------------------------------------------------
  • Jere Gmail ha scritto:
    > HI, I've been trying to do a batch converter for images. The problem
    > is the memory is not being released properly. When the for ends the
    > memory is ok, but during the for it continues increasing. I have a
    > function with a for loop:
    > for(count=0;count<cant;count++)
    > {
    > path=[datos objectAtIndex:count];
    > save_name=[NSString stringWithFormat:
    > @"image_%04d.jpg",count+1];
    >

    while the for() is running you're creating several instances of
    autoreleased objects like the NSString in the line above. These objects
    are not released until the control returns to the run loop.

    So if you don't want to keep on allocating memory during the loop, you
    can create an NSAutoreleasePool at the start and release it at the end.
    See
    http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concep
    ts/AutoreleasePools.html#//apple_ref/doc/uid/20000047


    --
    Simone Tellini
    http://tellini.info
  • thanks. it worked great just adding this to the loop
    NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
    ...loop code
    [loopPool release];

    On 9/21/07, Simone Tellini <cocoa-dev...> wrote:
    > Jere Gmail ha scritto:
    >> HI, I've been trying to do a batch converter for images. The problem
    >> is the memory is not being released properly. When the for ends the
    >> memory is ok, but during the for it continues increasing. I have a
    >> function with a for loop:
    >> for(count=0;count<cant;count++)
    >> {
    >> path=[datos objectAtIndex:count];
    >> save_name=[NSString stringWithFormat:
    >> @"image_%04d.jpg",count+1];
    >>
    >
    > while the for() is running you're creating several instances of
    > autoreleased objects like the NSString in the line above. These objects
    > are not released until the control returns to the run loop.
    >
    > So if you don't want to keep on allocating memory during the loop, you
    > can create an NSAutoreleasePool at the start and release it at the end.
    > See
    > http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concep
    ts/AutoreleasePools.html#//apple_ref/doc/uid/20000047

    >
    > --
    > Simone Tellini
    > http://tellini.info
    >
    >

    --
    http://zon7blog.blogspot.com/
    And again we fall.
previous month september 2007 next month
MTWTFSS
          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
Go to today