16

Could any one help me i want to add same image multiple times horizontally with same height and width. Important thing is i am creating image view dynamically i want to use same image view for all images! This is image enter image description here i want to make horizontally like this enter image description here but only one row needed like this.

Master Stroke
  • 5,108
  • 2
  • 26
  • 57
IamDev
  • 299
  • 4
  • 17

3 Answers3

17

You could achieve this by using stretchableImageWithLeftCapWidth:

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

As per your request:

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

And using your image: Small green part that needs to be repeated

The output is:

The output banner that has been created with the code

You can set this image on top of either UIScrollview, UIView and buttons. You do not need a for loop for that.

UPDATE:

The above code is for filling the entire background. If you wish to add only for one row then you have to create one UIView and set its colorWithPatternImage like below:

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

And the output:

The banner on top with white background below

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
4

Make a view of the height of image. But this view can have any width.

Then set your tile image in this view with following code.

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

This will get you the image tiled multiple times horizontally.

If the view spreads the image everywhere on screen then you'll have to add the following code to your view

 self.view.clipToBounds = YES;
CodenameLambda1
  • 1,299
  • 7
  • 17
  • Ni id on wanna apply background fully just need width 30,height 40 – IamDev Aug 23 '13 at 09:26
  • @iOSCode : Please note the 1st two lines of my answer here, I have explicitly said to "make a view of height of the image" and then in second line "set your tile image in this view". You are placing my code in viewDidLoad, which means, the image will be tiled on the whole view of viewController. – CodenameLambda1 Aug 23 '13 at 09:31
  • @iOSCoder i checked it is applying full background i don't need like tat – IamDev Aug 23 '13 at 09:32
  • @ Vijay and iOS Coder : Check my edit of the answer, I have added another line below. You have to set the clipToBounds property of your view to YES. – CodenameLambda1 Aug 23 '13 at 09:36
  • @CodenameLambda1..check my answer...have edited it...this is what i was trying to improve in your anwer...it was missing the direction,but logic is correct... – Master Stroke Aug 23 '13 at 09:51
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);

EDIT:

You can achieve this in one more way.

CodenameLambda1's answer is better than the above one.But still some changes needs to be done in the @CodenameLambda1's answer..as the SOP's requirement is to display it in scrollview.So instead of self.view use 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];
Master Stroke
  • 5,108
  • 2
  • 26
  • 57