2013-08-23 50 views
15

任何人都可以幫助我,我想要以相同的高度和寬度水平多次添加相同的圖像。重要的是我正在動態創建圖像視圖,我想爲所有圖像使用相同的圖像視圖!這是圖像enter image description here我想像這樣水平製作enter image description here,但只有一行需要這樣。如何在ios中動態創建水平重複圖像

+0

是你用scrollview嗎? –

+0

雅滾動使用 –

+0

你想實現什麼?問題不清楚... – preetam

回答

16

這與婁方法使用stretchableImageWithLeftCapWidth像這樣: -

UIImage *backgroundImage = [[UIImage imageNamed:@"SheetBackground.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0]; 

作爲您需要的示例: -

UIImage *backgroundImage = [[UIImage imageNamed:@"q4Ses.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0]; 

    [_scro setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]]; 

並使用您的圖片enter image description here

它的輸出,如: -

enter image description here

[在這裏輸入的形象描述] [4]

你可以設置圖片爲上述方式的任意UISCrollviewUIView,按鈕比肩!你不需要forloop

更新: -

這上面整個後臺代碼,如果你只想添加一個lineof行,那麼你必須創建一個的UIView與參數需要海特並添加設置它的colorWithPatternImage,並添加像貝婁

UIImage *backgroundImage = [[UIImage imageNamed:@"q4Ses.png"] 
    stretchableImageWithLeftCapWidth:1 topCapHeight:0]; 
UIView *v=[[UIView alloc] 
    initWithFrame:CGRectMake(0, 0, _scro.frame.size.width, 45)]; 
[v setBackgroundColor:[UIColor 
    colorWithPatternImage:backgroundImage]]; 
[_scro addSubview:v]; 

而且它的輸出是: -

enter image description here

3

查看圖像的高度。但是這個觀點可以有任何寬度。

然後用下面的代碼在這個視圖中設置你的瓷磚圖像。

UIImage *tiledImage = [UIImage imageNamed:@"myTiledImage.png"]; 
self.view.backgroundColor = [UIColor colorWithPatternImage:tiledImage]; 

這會讓你的圖像水平平鋪多次。

如果視圖到處散佈於屏幕上的圖像,那麼你就必須將以下代碼添加到您的視圖

self.view.clipToBounds = YES; 
+0

你是否確定這一點?...它不是橫向......在公園附近...... –

+0

你是什麼意思「四周公園」?我沒有讓你... – CodenameLambda1

+0

倪身份證上想申請背景完全只需要寬度30,身高40 –

0
UIScrollView *myScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; 

CGFloat scrollWidth = 0.f; 
for (int i=0;i<10;i++)//i=10,put as many image number u want to display 
{ 

    imageView = [[UIImageView alloc] initWithFrame: 
          CGRectMake(scrollWidth, 0, 80, 60.f)]; 
    imageView.image=[UIImage imageNamed:@"urimagename"]; 
    imageView.tag=i; 
    [myScrollView addSubview:imageView]; 

    scrollWidth += 100; 
} 
myScrollView.contentSize = CGSizeMake(scrollWidth, 100); 

編輯

您可以另一種方式實現這一目標。

CodenameLambda1的答案比上面的答案要好。但是仍然需要在@CodenameLambda1的答案中完成一些更改。由於SOP的要求是將其顯示在scrollview中,因此而不是self.view使用scrollview。

UIScrollView *vie=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)]; 
UIImage *tiledImage = [UIImage imageNamed:@"login"]; 
vie.backgroundColor = [UIColor colorWithPatternImage:tiledImage]; 
vie.contentSize=CGSizeMake(1400,60); 
vie.clipsToBounds = YES; 

[self.view addSubview:vie];