实现将NSImage保持指定像素的拉伸 发表于 2013-07-14 | 分类于 pieces 在UIKit中的UIImage有以下两个方法可以实现对图片的部分拉伸: 12- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets 但在AppKit中的NSImage却没有类似的方法,于是手动写了类似这两个功能的函数: 1234//保持四周一定区域像素不拉伸,将图像扩散到一定的大小- (NSImage *)stretchableImageWithSize:(NSSize)size edgeInsets:(NSEdgeInsets)insets;//保持leftWidth,rightWidth这左右一定区域不拉伸,将图片宽度拉伸到(leftWidth+middleWidth+rightWidth)- (NSImage *)stretchableImageWithLeftCapWidth:(float)leftWidth middleWidth:(float)middleWidth rightCapWidth:(float)rightWidth; 如图所示: Demo下载:StretchableDemo