簡體   English   中英

UIImageView autoresizingmask,UIImage stretchableImageWithLeftCapWidth

[英]UIImageView autoresizingmask, UIImage stretchableImageWithLeftCapWidth

我正在觀看一些WWDC視頻,他們談論使用UIImageView來拉伸你的圖像,不會產生任何額外的記憶。 他們的例子是使用一個小文本泡泡,就像你在音樂應用程序中長按tableViewCell時得到的那個。 然后他們討論如何將它分成3個切片,左,右和中心,因為中心部分底部有一個小三角形,指向你長按的tableViewCell。 有人可以解釋某人是如何做到的,因為他們沒有在他們的例子中提供任何代碼嗎? 偽代碼很好。 看一下文檔,我的猜測是:

從圖形編輯器創建三個最小尺寸的UIImages:

UIImage *leftImage = [UIImage imageNamed:@"left.png"];
UIImage *rightImage = [UIImage imageNamed:@"right.png"];
UIImage *centerImage = [UIImage imageNamed:@"center.png"];

leftImage = [leftImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];
rightImage = [rightImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];
centerImage = [centerImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];

UIImageView *leftImgView = [[UIImageView alloc] initWithImage:leftImage];
leftImgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UIImageView *rightImgView = [[UIImageView alloc] initWithImage:rightImage];
leftImgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UIImageView *centerImgView = [[UIImageView alloc] initWithImage:centerImage];
leftImgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

// do I just add them all to the subview of self.view?  Do I need to set their frames somewhere?  If so, how do I know where they go since it's 3 slices making up an image.
// release memory

我對這個主題的相關問題是你如何在IB中做到這一點。 就像你讓3個imageViews占據整個屏幕一樣,因為那是你想要的圖像擴展到什么?

實際上,您不需要創建三個圖像。 可伸展UIImage的關鍵在於它伸展中間但不是用戶可定義的末端。 您可以定義不希望被拉伸的像素的水平和垂直區域。

它使用起來非常簡單,這是一個例子:

UIImage *stretchyImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0]

然后將其設置為您想要的大小。 帶插圖的精美文檔在這里

編輯:我認為你必須以編程方式創建可伸縮的圖像,請參閱此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM