2012-01-31 63 views
0

我正在看一些WWDC視頻,他們談論使用UIImageView來拉伸你的圖像,不會產生任何額外的內存。他們的例子是使用一個小文本泡泡,就像您在Music應用程序中長按tableViewCell時所獲得的泡泡一樣。然後他們會談論如何將它分成3片,左邊,右邊和中間,因爲中間部分底部有一個小三角形,指向您長時間按下的tableViewCell。有人可以解釋某人如何做這件事,因爲他們沒有在他們的例子中提供任何代碼?僞代碼很好。綜觀文檔,我的猜測是:UIImageView autoresizingmask,UIImage stretchableImageWithLeftCapWidth

從圖形編輯器創建的最小尺寸三個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佔據整個屏幕,因爲這是你想要的圖像擴大到?

回答

2

其實,你不需要創建三個圖像。可伸縮UIImage的一點是它延伸了中間,但不是用戶可定義的結束。您可以定義不想伸展的像素的水平和垂直區域。

這是非常簡單的使用,這裏有一個例子:

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

然後,只需將其設置爲你想要的大小。帶插圖的好文檔是​​。

編輯: 我認爲你必須以編程方式創建可拉伸的圖像,請參閱here