1

I created drag and drop app using Apple's example: https://developer.apple.com/library/mac/samplecode/SourceView/Introduction/Intro.html

I load actual image of dragged file.

When I drag for example this enter image description here image file into my NSOutlineView, I see that it resized in this way:

enter image description here

I used as is without any modifications Apple's ImageAndTextCell class for custom NSTextFieldCell.

How I can resize this image to fit proportionally cell rect?

KAMIKAZE
  • 420
  • 6
  • 27
  • 1
    Please check this link : http://theocacao.com/document.page/498 – Sheen Vempeny Apr 23 '15 at 09:54
  • I tried it, but when I modified method - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView in ImageAndTextCell.m class to NSImage *resizedImage = [self.myImage imageByScalingProportionallyToSize:.... I have same results, nothing changed.. – KAMIKAZE Apr 23 '15 at 10:24
  • Cell-based tables are deprecated. I recommend you make the table view-based and then you can draw whatever you like in each row/col. – Rupert Pupkin Apr 23 '15 at 13:09
  • You mean in my NSOutlineView set option - Table View: "view based"? Currently I have "Cell Based" there. – KAMIKAZE Apr 23 '15 at 13:25
  • Yes, exactly. Here is the deprecation notice: https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKit/ – Rupert Pupkin Apr 23 '15 at 13:35
  • But when I set it - my app not works, I see only text "Table View Cell" instead of names of my objects. Also, this type of view will allow me to display tree structure? – KAMIKAZE Apr 23 '15 at 14:48

1 Answers1

0

Works perfect.

@implementation NSImage (ProportionalScaling)

- (NSImage*)imageByScalingProportionallyToSize:(NSSize)targetSize
{
  NSImage* sourceImage = self;
  NSImage* newImage = nil;

  if ([sourceImage isValid])
  {
    NSSize imageSize = [sourceImage size];
    float width  = imageSize.width;
    float height = imageSize.height;

    float targetWidth  = targetSize.width;
    float targetHeight = targetSize.height;

    float scaleFactor  = 0.0;
    float scaledWidth  = targetWidth;
    float scaledHeight = targetHeight;

    NSPoint thumbnailPoint = NSZeroPoint;

    if ( NSEqualSizes( imageSize, targetSize ) == NO )
    {

      float widthFactor  = targetWidth / width;
      float heightFactor = targetHeight / height;

      if ( widthFactor < heightFactor )
        scaleFactor = widthFactor;
      else
        scaleFactor = heightFactor;

      scaledWidth  = width  * scaleFactor;
      scaledHeight = height * scaleFactor;

      if ( widthFactor < heightFactor )
        thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

      else if ( widthFactor > heightFactor )
        thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
    }

    newImage = [[NSImage alloc] initWithSize:targetSize];

    [newImage lockFocus];

      NSRect thumbnailRect;
      thumbnailRect.origin = thumbnailPoint;
      thumbnailRect.size.width = scaledWidth;
      thumbnailRect.size.height = scaledHeight;

      [sourceImage drawInRect: thumbnailRect
                     fromRect: NSZeroRect
                    operation: NSCompositeSourceOver
                     fraction: 1.0];

    [newImage unlockFocus];

  }

  return [newImage autorelease];
}

@end
KAMIKAZE
  • 420
  • 6
  • 27