2013-12-10 41 views
1

我有像圖像氣泡,我需要動態調整大小。 附加示例。 我將有動態文本,我只需要調整泡沫的直線部分。在中間留下箭頭 我該怎麼做? 感謝UIImage如何用capInsets調整泡泡圖像的大小?

enter image description here

+0

可能的重複:http://stackoverflow.com/questions/20117787/custom-resizable-image-drawing-in-ios –

回答

1

我認爲resizableImageWithCapInsets:會爲你

例子非常有用:

UIImage *image = [[UIImage imageNamed:@"buttonImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0,10.0,5.0,10.0)]; 
[self.yourButton setBackgroundImage:image forState:UIControlStateNormal]; 

我寫了按鈕的例子,因爲它是使用這種方法的一個常見的情況。只玩限制值。

HTH!

編輯:

想過這個問題,寫了小例子。 我明白,resizableImageWithCapInsets:可以保存圖像的角落,以防我們使用二維尺度。

我看到2種方式:

  1. 使用兩個控件:一個用於氣球和一個箭頭適當的圖像
  2. 從兩個圖像

我決定渲染這一個控制圖像實現第二個。 通ballonView.frame作爲參數在這裏:

- (UIImage *)balloonImageWithRect:(CGRect)rect{ 
    //create two images, ballon image with cap corners 
    UIImage *ballonImage = [[UIImage imageNamed:@"balloon"] resizableImageWithCapInsets:UIEdgeInsetsMake(IMAGE_CAP_INSET_TOP, IMAGE_CAP_INSET_LEFT, IMAGE_CAP_INSET_BOTTOM, IMAGE_CAP_INSET_RIGHT) resizingMode:UIImageResizingModeStretch]; 
    UIImage *arrowImage = [UIImage imageNamed:@"arrow"]; 

    //drawing area 
    CGSize newSize = rect.size; 
    UIGraphicsBeginImageContext(newSize); 

    //leave some space for arrow at the bottom 
    [ballonImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height - arrowImage.size.height)]; 
    //draw arrow at the bottom 
    [arrowImage drawInRect:CGRectMake((newSize.width - arrowImage.size.width)/2, newSize.height - arrowImage.size.height, arrowImage.size.width, arrowImage.size.height)]; 

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

然後我們就這個形象設定爲我們的ballonView。

而且我離開要點與該氣囊的隨機尺寸是視圖控制器的全碼:https://gist.github.com/AlloyDev/7910637

您也可以使用兩個圖像:

balloon image arrow image

+0

它調整大小,但我不能調整隻有左側和右側的部分,沒有改變箭頭中間 – Anton

+0

@安頓你必須蓋上你的箭頭,提高你的底部價值。 – kaspartus

+0

@安頓本教程也可能對你有用http://iosdevelopertips.com/user-interface/ios-5-uiimage-and-resizableimagewithcapinsets.html – kaspartus

1

您可以使用方法:

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight; 

這樣的電話號碼:

UIImage* image = [[UIImage imageNamed:@"bubbleImageName.png"] 
         stretchableImageWithLeftCapWidth:15 topCapHeight:13]; 

變化capWidth和大寫高度重視

+0

這幫了我。謝謝 :) –

0

使用UIImage.h委託方法

//使用resizableImageWithCapInsets:和capInsets。

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight; 
@property(nonatomic,readonly) NSInteger leftCapWidth; // default is 0. if non-zero, horiz. stretchable. right cap is calculated as width - leftCapWidth - 1 
@property(nonatomic,readonly) NSInteger topCapHeight; // default is 0. if non-zero, vert. stretchable. bottom cap is calculated as height - topCapWidth - 1 

這樣

UIImage *balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15]; 

希望這會有所幫助。

0

對於問題的圖像(假設它是一個視網膜圖像):

[[UIImage imageNamed:@"Bubble"] resizableImageWithCapInsets:UIEdgeInsetsMake(14, 28, 14, 26) resizingMode:UIImageResizingModeStretch]; 

如果你在問題中所提供的圖像是一個非視網膜圖像,減少一半的值。

修改值(UIEdgeInsetsMake(top, left, bottom, right))以適合您需要的任何圖像。

相關問題