Repeat an Image according to count - ios

Basically I need to display "Ratings" with the help of image, I need to repeat an Image according to number count. Like if variable count is 3 then image will be shown thrice. How to do this?

You can run for loop and add images as subview to some view.
-(UIView*)createRatingView :(NSInteger)stars {
UIView* rating = [[UIView alloc]initWithFrame:CGRectMake(0,0,120,20)]; //set proper frame.. In this example (20*5) + (5*4) where 20 is width of image and 5 is padding between stars
for (i=1;i<=5;i++) {
int xPos = (i-1)*(20+5); // assuming 20 is width of image and 5 is padding between 2 stars
UIImageView* img = [[UIImageView alloc]initWithFrame:CGRectMake(xPos,0,20,20)];
if (stars<= i) {
img.image = [UIImage named:#"goldStar"];
}
else {
img.image = [UIImage named:#"grayStar"];
}
[rating addSubview : img];
}
return rating;
}

Related

Issue with subviews overlapping

I'm trying to create a custom view which containes several images. I do that by adding them programmatically. The problem is that those subviews overlap each other and I can't find the way to change that. The only solution I can see is doing something like setting frames for each new image programmatically. I would be grateful if someone could tell me what is the best way to solve this issue.
for (id image in self.images) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.imageViews addObject:imageView];
[self addSubview:imageView];
}
If you wanna make your customView like UICollectionView you need a UIScrollView and add your subviews in it. Everytime when you add a subview change frame location so it could be something like this:
int xPosition = 0;
int yPosition =0;
for (id image in self.images) {
if (xPosition>self.view.frame.size.width) {
//give size to every imageView and every time in loop change the location
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPosition, yPosition, 50, 50)];
imageView.image = image;
yPosition = yPosition + 50;
[self.view addSubView:imageView];
}
else {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPosition, yPosition, 50, 50)];
imageView.image = image;
xPosition = xPosition + 50;
[self.view addSubView:imageView];
}
}
Without using Interface Builder your only real options are to change the frame or the center.
imageView.frame = CGRectMake(x coord, y coord, width, height);
This method lets you resize and move whereas changing the center lets you do just that, move the center of the view.
or
imageView.center = CGPointMake(x coord, y coord);
Or as recommended add constraints.

Spawn UIImageView So That It Does Not Intersect With Existing UIImageViews

Basically, I have an app where a user dodges bombs and collects coins. When a user collects a coin, another coin is spawned. I want each coin to not spawn on a bomb. (By the way these are all uiimageviews). In the code below, I have an array of my bombs called bombArray and the UIImageview called "one" is the coin UIImageView. I know the code below doesn't work, but what other method(s) could I use? Thanks, and here is the code:
UIImageView *one = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"goldCoin.png"]];
CGRect rectOne = CGRectMake(arc4random() % (900), arc4random() % (700), 40, 40);
[one setFrame:rectOne];
[self.view addSubview:one];
for (UIImageView* two in bombArray)
{
while (CGRectIntersectsRect(two.frame, one.frame))
{
one.center=CGPointMake(arc4random() % (900), arc4random() % (700));
}
}
*Note: I completely understand why the code above does not work, but I cannot find another solution to my problem.
First of all to make such a thing I'd use the new SpriteKit that gives you physics (and thus collision detection) out of the box.
But if you want to do this with standard UIViews what you can do for being absolutely sure that images don't collide between them is to divide the container in a grid and then loop through each cell of your grid and "flip a coin" to randomly put a coin or not in that cell. Then if you want to fill more space instead of dividing for rectangular cells you can divide for hexagonal cells, in this way you will cover more space.
So a fast and absolutely not too much checked version could be something like this:
int x = 0;
int y = 0;
int side = 40;
int it = 0;
do {
do {
CGRect imageFrame = CGRectMake(x, y, side, side);
UIImageView* imageView = [[UIImageView alloc] initWithFrame:imageFrame];
imageView.layer.borderColor = [UIColor blackColor].CGColor;
imageView.layer.borderWidth = 1.0;
imageView.layer.cornerRadius = side / 2;
imageView.layer.masksToBounds = YES;
[self.containerView addSubview:imageView];
BOOL flip = arc4random() & 0x1;
if (flip) {
[imageView setImage:[UIImage imageNamed:#"coin_image"]];
}
} while ((x+=side) < (self.containerView.frame.size.width - side / 2));
it++;
if (it % 2 != 0) {
x = side / 2;
} else {
x = 0;
}
} while ((y+=(side * 0.85)) < (self.containerView.frame.size.height - side) );
Mind that this is NOT efficient, it is executed in the main thread and doesn't take in account possible (actually probable) memory constraints.

Using a String as Images Name

I am working on a while loop that spits out 30 images, each image having a different name. The way I approached this was by creating a NSString variable call img, which will be different with each iteration, i.e., "Badguy 1", "Badguy 2", ect... Then using that string as the name of the image being created.
TotalShips = 1
while(TotalShips < 31){
img = [NSString stringWithFormat:#"Badguy %i",TotalShips];
UIImageView *img = [[UIImageView alloc] initWithFrame: CGRectMake(10,20,20,20)];
img.image = [UIImage imageNamed:#"Badguy.png"];
[self.view addSubview: img];
TotalShips = TotalShips + 1;
}
This doesn't seem to work, and I haven't found very much help on changing an image's name.
Thanks,
Alex
The UIImageView has an property animatedImages (NSArray). Vou can easily play auround with animationDuration and animationReapCount. You can start the animation with [myImageView startAnimation] and stop with [myImageView stopAnimation].
Please read: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImageView_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006889-CH3-SW3
The file names can you build in a loop with 'NSString *fileName = [NSString stringWithFormat:#"image%i.png",imageName, iterator]'.
Not sure what you're trying to do, but I'll try to explain what your code does, maybe it helps you understand the problem:
TotalShips = 1
while(TotalShips < 31){
// here your img variable is a string, which will be 'Badguy 1', 'Badguy 2', etc
// you're not using this img value set here anywhere else in your code
img = [NSString stringWithFormat:#"Badguy %i",TotalShips];
// here you redeclare your img as an UIImageView
UIImageView *img = [[UIImageView alloc] initWithFrame: CGRectMake(10,20,20,20)];
// here you assign the image in the imageView to the image 'Badguy.png'
img.image = [UIImage imageNamed:#"Badguy.png"];
[self.view addSubview: img];
TotalShips = TotalShips + 1;
}
So, your code creates 30 different UIImageViews , all of them with the same image: 'Badguy.png', and adds them one on top of the other to the current view. I suppose this is not what you wanted to do.

How to add image in image view ios

I have a view controller,on top of this I have added a image view.Now I wanted to load some image dynamically to that image view.Fallowing is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if (getAnySelectedRow == 0)
{
//Load corresponding image
NSLog(#"1 image loaded");
}
else if (getAnySelectedRow == 1)
{
//Load corresponding image
NSLog(#"2 image loaded");
}
else if (getAnySelectedRow == 2)
{
//Load corresponding image
NSLog(#"3 image loaded");
}
}
Note: Image view is already loaded on top of view controller.So I believe no need to init new image view.
How can I do this?
This is actually quite simple. UIImageView, has an image property of type UIImage. All you have to do is create a UIImage object and then assign it to this property. If the image exists within your applications bundle, you can use +[UIImage imageNamed:]`.
myImageView.image = [UIImage imageNamed:#"myImageName"];
Or to expand a little further..
if (getAnySelectedRow == 0) {
myImageView.image = [UIImage imageNamed:#"myImageName"];
}else if (getAnySelectedRow == 1) {
myImageView.image = [UIImage imageNamed:#"myOtherImageName"];
}else{
myImageView.image = [UIImage imageNamed:#"myAnotherDifferentImageName"];
}
x, y are the position the image view located,
width and height are the size of the ImageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
imageView.image = [UIImage imageNamed:#"your image"];

Setting the size for an image in a TableViewCell

I have a table with rows of height 90.
I want to have an image on each row of size 50x50.
I am doing the following in cellForRowAtIndexPath:
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
if (item.image){
cell.imageView.image = [item.image smallImage];
cell.imageView.alpha = 1.0f;
} else {
cell.imageView.image = [UIImage imageNamed:#"icon-greyed.png"];
cell.imageView.alpha = 0.25f;
}
However the images appear with height 90 (the height of the row). What am I not doing right?
You will need to either add a UIImageView to cell.contentView which I wouldn't recommend, or create a custom UITableViewCell, which I would recommend. Then you can add a UIImageView to your custom cell and resize it to the dimensions you require.

Resources