完美的作品。
@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
請檢查此鏈接:http://theocacao.com/document.page/498 –
我試了一下,但是當我修改方法 - (無效)drawWithFrame:(的NSRect)cellFrame inView:(*的NSView)在ImageAndTextCell.m類中的controlView到NSImage * resizedImage = [self.myImage imageByScalingProportionallyToSize:....我有相同的結果,沒有任何更改.. – KAMIKAZE
基於單元格的表已棄用。我建議你製作基於表格的視圖,然後你可以在每行/列中繪製任何你喜歡的東西。 –