Gluing two images together question...

  • Hi,

    I am trying to add a "display two images side by side" feature to
    one of my apps.

    The following snippet does the job by the resulting image is a
    low res image does not zoom without pixelation. Is there a
    way to glue two images together into a single image that keeps
    the approximate resolution of the components?

    In the following code each of tmp1 and tmp2 are jpgs that
    can be zoomed a great deal and not show pixelation.

    The resulting image "displayed" image winds up have
    pixel size = point size. Not good for zooming ;(

    Thanks

    Jerry

        NSImage * tmp1 = [[NSImage alloc] initWithContentsOfFile:
    (NSString*) [fileNameList objectAtIndex:slideNumber]];
        NSImage * tmp2 = [[NSImage alloc] initWithContentsOfFile:
    (NSString*) [fileNameList objectAtIndex:nextSlide]];
        int x1 = [tmp1 size].width; int x2 = [tmp2 size].width;
        int y1 = [tmp1 size].height; int y2 = [tmp2 size].height;
        float newHeight;
        if (y1 > y2){
            newHeight = y1;
         }
    else {
         newHeight=y2;
    }
        displayedImage = [[NSImage alloc] initWithSize:
    NSMakeSize(x1+x2,newHeight)];
        [displayedImage lockFocus];
          [tmp1 drawAtPoint:NSMakePoint(0.0,0.0) fromRect:
    NSMakeRect(0,0,x1,y1) operation:NSCompositeCopy fraction:1.0];
          [tmp2 drawAtPoint:NSMakePoint(x1,0.0) fromRect:
    NSMakeRect(0,0,x2,y2) operation:NSCompositeCopy fraction:1.0];
        [displayedImage unlockFocus];
    //NSLog(@"dispImg=%@" ,displayedImage);
          [tmp1 release]; [tmp2 release];
  • Hi Jerry,

    I think you'd be best off not attempting to glue the images together.
    Just hold onto both of them, and draw them next to each other,
    probably by writing a custom subclass of NSView.  This will preserve
    all the quality, and is likely to perform better, depending perhaps on
    what else you're doing with the images.

    If you have a particular need to pack the two images up as an NSImage,
    you could do this by making a custom subclass of NSImageRep,
    CompositeImageRep, that holds onto the two images and draws them both
    when asked to draw itself.  This is not very difficult, though it
    sounds like it might be.  Check the docs for NSImage, NSImageRep, and
    the conceptual documentation for more info.

    -Ken
    Cocoa Frameworks

    On Dec 17, 2007 4:11 PM, Jerry LeVan <jerry.levan...> wrote:
    > Hi,
    >
    > I am trying to add a "display two images side by side" feature to
    > one of my apps.
    >
    > The following snippet does the job by the resulting image is a
    > low res image does not zoom without pixelation. Is there a
    > way to glue two images together into a single image that keeps
    > the approximate resolution of the components?
    >
    > In the following code each of tmp1 and tmp2 are jpgs that
    > can be zoomed a great deal and not show pixelation.
    >
    > The resulting image "displayed" image winds up have
    > pixel size = point size. Not good for zooming ;(
    >
    > Thanks
    >
    > Jerry
    >
    > NSImage * tmp1 = [[NSImage alloc] initWithContentsOfFile:
    > (NSString*) [fileNameList objectAtIndex:slideNumber]];
    > NSImage * tmp2 = [[NSImage alloc] initWithContentsOfFile:
    > (NSString*) [fileNameList objectAtIndex:nextSlide]];
    > int x1 = [tmp1 size].width; int x2 = [tmp2 size].width;
    > int y1 = [tmp1 size].height; int y2 = [tmp2 size].height;
    > float newHeight;
    > if (y1 > y2){
    > newHeight = y1;
    > }
    > else {
    > newHeight=y2;
    > }
    > displayedImage = [[NSImage alloc] initWithSize:
    > NSMakeSize(x1+x2,newHeight)];
    > [displayedImage lockFocus];
    > [tmp1 drawAtPoint:NSMakePoint(0.0,0.0) fromRect:
    > NSMakeRect(0,0,x1,y1) operation:NSCompositeCopy fraction:1.0];
    > [tmp2 drawAtPoint:NSMakePoint(x1,0.0) fromRect:
    > NSMakeRect(0,0,x2,y2) operation:NSCompositeCopy fraction:1.0];
    > [displayedImage unlockFocus];
    > //NSLog(@"dispImg=%@" ,displayedImage);
    > [tmp1 release]; [tmp2 release];
    >
  • Doh, I got hit by a clue bat last night...

    Instead of gluing the high res photos and compositing and then
    doing the zoom ( the code below) simply
    simply zoom the images and *then* glue and composite
    the result!

    Works a charm :)

    Jerry

    On Dec 18, 2007, at 7:09 PM, Ken Ferry wrote:

    > Hi Jerry,
    >
    > I think you'd be best off not attempting to glue the images together.
    > Just hold onto both of them, and draw them next to each other,
    > probably by writing a custom subclass of NSView.  This will preserve
    > all the quality, and is likely to perform better, depending perhaps on
    > what else you're doing with the images.
    >
    > If you have a particular need to pack the two images up as an NSImage,
    > you could do this by making a custom subclass of NSImageRep,
    > CompositeImageRep, that holds onto the two images and draws them both
    > when asked to draw itself.  This is not very difficult, though it
    > sounds like it might be.  Check the docs for NSImage, NSImageRep, and
    > the conceptual documentation for more info.
    >
    > -Ken
    > Cocoa Frameworks
    >
    > On Dec 17, 2007 4:11 PM, Jerry LeVan <jerry.levan...> wrote:
    >> Hi,
    >>
    >> I am trying to add a "display two images side by side" feature to
    >> one of my apps.
    >>
    >> The following snippet does the job by the resulting image is a
    >> low res image does not zoom without pixelation. Is there a
    >> way to glue two images together into a single image that keeps
    >> the approximate resolution of the components?
    >>
    >> In the following code each of tmp1 and tmp2 are jpgs that
    >> can be zoomed a great deal and not show pixelation.
    >>
    >> The resulting image "displayed" image winds up have
    >> pixel size = point size. Not good for zooming ;(
    >>
    >> Thanks
    >>
    >> Jerry
    >>
    >> NSImage * tmp1 = [[NSImage alloc] initWithContentsOfFile:
    >> (NSString*) [fileNameList objectAtIndex:slideNumber]];
    >> NSImage * tmp2 = [[NSImage alloc] initWithContentsOfFile:
    >> (NSString*) [fileNameList objectAtIndex:nextSlide]];
    >> int x1 = [tmp1 size].width; int x2 = [tmp2 size].width;
    >> int y1 = [tmp1 size].height; int y2 = [tmp2 size].height;
    >> float newHeight;
    >> if (y1 > y2){
    >> newHeight = y1;
    >> }
    >> else {
    >> newHeight=y2;
    >> }
    >> displayedImage = [[NSImage alloc] initWithSize:
    >> NSMakeSize(x1+x2,newHeight)];
    >> [displayedImage lockFocus];
    >> [tmp1 drawAtPoint:NSMakePoint(0.0,0.0) fromRect:
    >> NSMakeRect(0,0,x1,y1) operation:NSCompositeCopy fraction:1.0];
    >> [tmp2 drawAtPoint:NSMakePoint(x1,0.0) fromRect:
    >> NSMakeRect(0,0,x2,y2) operation:NSCompositeCopy fraction:1.0];
    >> [displayedImage unlockFocus];
    >> //NSLog(@"dispImg=%@" ,displayedImage);
    >> [tmp1 release]; [tmp2 release];
    >> _______________________________________________
    >>
previous month december 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
31            
Go to today