實現將NSImage保持指定畫素的拉伸

2021-07-25 13:41:50 字數 2988 閱讀 9597

**:

- (uiimage *)stretchableimagewithleftcapwidth:(nsinteger)leftcapwidth topcapheight:(nsinteger)topcapheight

- (uiimage *)resizableimagewithcapinsets:(uiedgeinsets)capinsets

//保持四周一定區域畫素不拉伸,將影象擴散到一定的大小

- (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

;

nsrect rect = nsmakerect(0, 0, size.width, size.height);

// source rects

nsrect srcrect = (nsrect);

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 = ;

nsrect dstrects = ;

nsmutablearray *partimgs = [nsmutablearray arraywithcapacity:9];

for (int i=0;i<9;i++)

// 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

// generate the middle image

nsrect rectmiddle = nsmakerect(0, 0, middlewidth, imageheight);

nsimage *imagemiddle = [[nsimage alloc] initwithsize:rectmiddle.size];

if (imagemiddle.size.width > 0)

// generate the right image

nsrect rectright = nsmakerect(0, 0, rightwidth, imageheight);

nsimage *imageright = [[nsimage alloc] initwithsize:rectright.size];

if (imageright.size.width > 0)

// combine the images

nsimage *newimage = [[nsimage alloc] initwithsize:nsmakesize(imagewidth, imageheight)];

if (newimage.size.width > 0)

// release the images and return the new image

return newimage;

}

Javascript實現將元素插入到指定位置

主要用到splice 方法,splice 方法可刪除從 index 處開始的零個或多個元素,並且用引數列表中宣告的乙個或多個值來替換那些被刪除的元素。1.陣列指定位置插入元素 var array one two four 原來的陣列 array.splice 2,0,three splice pos...

用python實現將檔案拷貝到指定目錄

import os import shutil alllist os.listdir u d notes python 資料 for i in alllist aa,bb i.split if python in aa.lower oldname u d notes python 資料 aa bb ...

VC實現將資源打包並釋放到指定資料夾

很多時候,我們可能要將某些檔案打包到資源中,然後當程式執行的時候,發現缺少某些檔案時,進行自我修復,以維持程式的正常執行,很多安裝程式或者比較高階點的智慧型程式都已經這樣做,比如檔案損壞時,安裝程式將修復這些檔案.一些高階程式的常用設定及檔案 遇到破壞時,也會自我修復,它們的這些神奇功能,全都是依靠...