实现将NSImage保持指定像素的拉伸
来源:互联网 发布:博世待遇怎么样 知乎 编辑:程序博客网 时间:2024/06/05 10:35
转自:http://www.tanhao.me/pieces/1408.html/
在UIKit中的UIImage有以下两个方法可以实现对图片的部分拉伸:
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
但在AppKit中的NSImage却没有类似的方法,于是手动写了类似这两个功能的函数:
//保持四周一定区域像素不拉伸,将图像扩散到一定的大小- (NSImage *)stretchableImageWithSize:(NSSize)size edgeInsets:(NSEdgeInsets)insets;//保持leftWidth,rightWidth这左右一定区域不拉伸,将图片宽度拉伸到(leftWidth+middleWidth+rightWidth)- (NSImage *)stretchableImageWithLeftCapWidth:(float)leftWidth middleWidth:(float)middleWidth rightCapWidth:(float)rightWidth;
详细代码如下:
/** * //保持四周一定区域像素不拉伸,将图像扩散到一定的大小 */- (NSImage *)stretchableImageWithSize:(NSSize)size edgeInsets:(NSEdgeInsets)insets{ void (^makeAreas)(NSRect, NSRect *, NSRect *, NSRect *, NSRect *, NSRect *, NSRect *, NSRect *, NSRect *, NSRect *) = ^(NSRect srcRect, NSRect *tl, NSRect *tc, NSRect *tr, NSRect *ml, NSRect *mc, NSRect *mr, NSRect *bl, NSRect *bc, NSRect *br) { CGFloat w = NSWidth(srcRect);//60 CGFloat h = NSHeight(srcRect);//62 CGFloat cw = w - insets.left - insets.right;//60 - 15 - 15 = 30 CGFloat ch = h - insets.top - insets.bottom; //62 - 20 -20 = 22 CGFloat x0 = NSMinX(srcRect);//0 CGFloat x1 = x0 + insets.left;//15 CGFloat x2 = NSMaxX(srcRect) - insets.right;// 60 - 15 = 45 CGFloat y0 = NSMinY(srcRect);//0 CGFloat y1 = y0 + insets.bottom;//20 CGFloat y2 = NSMaxY(srcRect) - insets.top;//62-20=42 *tl = NSMakeRect(x0, y2, insets.left, insets.top); *tc = NSMakeRect(x1, y2, cw, insets.top); *tr = NSMakeRect(x2, y2, insets.right, insets.top); *ml = NSMakeRect(x0, y1, insets.left, ch); *mc = NSMakeRect(x1, y1, cw, ch); *mr = NSMakeRect(x2, y1, insets.right, ch); *bl = NSMakeRect(x0, y0, insets.left, insets.bottom); *bc = NSMakeRect(x1, y0, cw, insets.bottom); *br = NSMakeRect(x2, y0, insets.right, insets.bottom); }; NSRect rect = NSMakeRect(0, 0, size.width, size.height); // Source rects NSRect srcRect = (NSRect){NSZeroPoint, self.size}; NSRect srcTopL, srcTopC, srcTopR, srcMidL, srcMidC, srcMidR, srcBotL, srcBotC, srcBotR; makeAreas(srcRect, &srcTopL, &srcTopC, &srcTopR, &srcMidL, &srcMidC, &srcMidR, &srcBotL, &srcBotC, &srcBotR); // Destinations rects NSRect dstTopL, dstTopC, dstTopR, dstMidL, dstMidC, dstMidR, dstBotL, dstBotC, dstBotR; makeAreas(rect, &dstTopL, &dstTopC, &dstTopR, &dstMidL, &dstMidC, &dstMidR, &dstBotL, &dstBotC, &dstBotR); NSRect srcRects[] = {srcTopL, srcTopC, srcTopR, srcMidL, srcMidC, srcMidR, srcBotL, srcBotC, srcBotR}; NSRect dstRects[] = {dstTopL, dstTopC, dstTopR, dstMidL, dstMidC, dstMidR, dstBotL, dstBotC, dstBotR}; NSMutableArray *partImgs = [NSMutableArray arrayWithCapacity:9]; for (int i=0;i<9;i++) { NSRect aSrcRect = srcRects[i]; NSRect aDstRect = dstRects[i]; NSImage *partImg = [[NSImage alloc] initWithSize:aSrcRect.size]; [partImg lockFocus]; [self drawAtPoint:NSZeroPoint fromRect:aSrcRect operation:NSCompositeSourceOver fraction:1.0]; [partImg setSize:aDstRect.size]; [partImg unlockFocus]; [partImgs addObject:partImg]; } // Draw NSImage *resultImg = [[NSImage alloc] initWithSize:rect.size]; [resultImg lockFocus]; NSDrawNinePartImage(rect, [partImgs objectAtIndex:0], [partImgs objectAtIndex:1], [partImgs objectAtIndex:2], [partImgs objectAtIndex:3], [partImgs objectAtIndex:4], [partImgs objectAtIndex:5], [partImgs objectAtIndex:6], [partImgs objectAtIndex:7], [partImgs objectAtIndex:8], NSCompositeSourceOver, 1, NO); [resultImg unlockFocus]; return resultImg;}/** * //保持leftWidth,rightWidth这左右一定区域不拉伸,将图片宽度拉伸到(leftWidth+middleWidth+rightWidth) */- (NSImage *)stretchableImageWithLeftCapWidth:(float)leftWidth middleWidth:(float)middleWidth rightCapWidth:(float)rightWidth{ // Calculate the new images dimensions float imageWidth = leftWidth + middleWidth + rightWidth; float imageHeight = self.size.height; // Generate the left image NSRect rectLeft = NSMakeRect(0, 0, leftWidth, imageHeight); NSImage *imageLeft = [[NSImage alloc] initWithSize:rectLeft.size]; if (imageLeft.size.width > 0) { [imageLeft lockFocus]; [self drawInRect:rectLeft fromRect:rectLeft operation:NSCompositeCopy fraction:1.0]; [imageLeft unlockFocus]; } // Generate the middle image NSRect rectMiddle = NSMakeRect(0, 0, middleWidth, imageHeight); NSImage *imageMiddle = [[NSImage alloc] initWithSize:rectMiddle.size]; if (imageMiddle.size.width > 0) { [imageMiddle lockFocus]; [self drawInRect:rectMiddle fromRect:NSMakeRect(leftWidth, 0, self.size.width-rightWidth-leftWidth,imageHeight) operation:NSCompositeCopy fraction:1.0]; [imageMiddle unlockFocus]; } // Generate the right image NSRect rectRight = NSMakeRect(0, 0, rightWidth, imageHeight); NSImage *imageRight = [[NSImage alloc] initWithSize:rectRight.size]; if (imageRight.size.width > 0) { [imageRight lockFocus]; [self drawInRect:rectRight fromRect:NSMakeRect(self.size.width - rightWidth, 0, rightWidth, imageHeight) operation:NSCompositeCopy fraction:1.0]; [imageRight unlockFocus]; } // Combine the images NSImage *newImage = [[NSImage alloc] initWithSize:NSMakeSize(imageWidth, imageHeight)]; if (newImage.size.width > 0) { [newImage lockFocus]; NSDrawThreePartImage(NSMakeRect(0, 0, imageWidth, imageHeight), imageLeft, imageMiddle, imageRight, NO, NSCompositeSourceOver, 1, NO); [newImage unlockFocus]; } // Release the images and return the new image return newImage;}
0 0
- 实现将NSImage保持指定像素的拉伸
- 【Mac OS X开发】实现NSImage对其指定像素的保持及拉伸
- 拉伸图片的指定区域
- NSImage的坐标系统
- NSImage的作用
- NSImage的缓存
- NSImage
- 在指定的位置拉伸图片
- 如何可以将图片生成为指定300*300像素的图片文件?
- js实现盒子的拉伸
- 对NSImage的一个扩展
- NSimage的size和pixels
- 对NSImage的一个扩展
- UIImageView、UIView、拉伸像素点
- Android 按指定尺寸 裁剪或拉伸图片的方法
- 怎样实现表格背景图片的拉伸效果
- 用滤镜实现背景图片的拉伸
- HTML5实现一根拉伸的橡皮筋
- 利用MAP文件精确定位代码中出错的代码行
- 日期工具类
- CSP设计之-设备连接
- 彻底清除Linux centos minerd木马
- linux下多进程共享资源互斥访问实例
- 实现将NSImage保持指定像素的拉伸
- [转]Python MySQLdb在Linux下的快速安装
- 理解长短期记忆网络(LSTM NetWorks)
- Hibernate 一对多映射 知识点。
- ajax技术前后台(mvc)
- Windows服务安装、卸载
- Markdown使用方法做个记录
- Android 自定义数字键盘(一)
- Xcode8升级后,必须注意的问题.