Repeat image horizontally and vertically - ios

I want to repeat the following image:
to achieve the following background:
I tried a few codes as follow:
bluePatternView.backgroundColor = [UIColor colorWithPatternImage:
[[UIImage imageNamed:#"blue_pattern.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0]];
and:
bluePatternView.backgroundColor =
[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"blue_pattern.png"]];
I also tried to draw image with CGContextDrawTiledImage with no success.
How that can be done?!
EDIT: result of implementing luk2302. please give him recognition by upvoting
bluePatternView.backgroundColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:#"blue_pattern.png"]];
result:

I found solution in images.xcassets,
Step 1:- Put your image to images.xcassets
Step 2:- Click on image, then click on bottom right corner written “Show Slicing”
Step 3:- Click on Start Slicing
Step 4:- Click on "Slice Horizontally and Vertically" button
Step 5:- Here you will see 3-Horizontal and 3-Vertical slice lines.
Put the most left vertical line to left side of image and both right most lines to right side of image.
Put the most top line to top of image and both bottom most lines to bottom of image.
So the final result will be looking like this.
Step 6:- Use this image.
And now image will be repeated.
Note:- If you give slicing to 2x image it will repeat just 2x image, for 3x images you need to do the same slicing.
Example by Apple

As my comment already said: use
bluePatternView.backgroundColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:#"blue_pattern.png"]];
You dont want to stretch your image. Let UIKit take care of the repetition.
Assuming that your bluePatternView is the actual large view for which you want to set a pattern as background. Maybe you are setting the background property of the wrong view if that code is still not working.
Note: duplicating the SAME image with #2x and #3x extension will lead to behaive properly for better resolution devices.

bluePatternView.backgroundColor = UIColor(patternImage: UIImage(named: "blue_pattern")!)

You need to save image in Assets then put those lines in viewdidload
bluePatternView.backgroundColor = UIColor.init(patternImage: #imageLiteral(resourceName: "f8"))
This is for Swift 4.1.

this with Bellow Method using stretchableImageWithLeftCapWidth like this way:-
UIImage *backgroundImage = [[UIImage imageNamed:#"SheetBackground.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0];
as par your need example:-
UIImage *backgroundImage = [[UIImage imageNamed:#"q4Ses.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0];
[_scro setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];

Related

iOS - Pull right view image of a text field from borders

In my iOS project, i added an image to the right of text field. It leaned on borders at all.
Then i searched and find this topic and used TTillage's solution. It became better, but still leaned on top and bottom borders.
Finally i changed the frame of my image, however pulling inside from right border was not working at this point. My final view is:
I want this little image to be a bit away from borders and not stretched (if i do second version with third, it gets stretched). Is there a way for this?
My code:
myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
myImage.frame = CGRectMake(30.0, 0.0, self.textField.frame.size.height-5.0, self.textField.frame.size.height-5.0);
//myImage.contentMode = UIViewContentModeCenter;
self.textField.rightViewMode = UITextFieldViewModeUnlessEditing;
self.textField.rightView = myImage;
Thank you.

how to implement this animation in iOS?

I am not sure about how to present this question because I don't know the animation term that I should use.
I need to know about this tree presentation animation. As it is appearing form root to top.
Please take a look on attached .gif file and let me know if anyone know about this animation or if you can guide me with example.
I will really appreciate.
Thanks for your time.
The best option for you is to split up the gif into multiple images and then do the following:
NSArray *gifImagesArray = [NSArray arrayWithObjects:imageOne, imageTwo, imageThree, nil];
imageView.animationImages = animateImagesArray;
imageView.animationRepeatCount = 1;
imageView.animationDuration = 1.0f;
[imageView startAnimating];
Edit following comments:
If you can't use multiple images you have to write the animation yourself.
One suggestion is to add a circular mask to the UIImage and animate it's removal.
This link explains how to draw a circular CALayer: Circular Progress Bars in IOS
This one here will show you how to create a mask on a UIImage: Simply mask a UIView with a rectangle
now all you have to do is a simple animation.
I have a solution that will work for this problem.
We can use gif image and convert it into UIImage object. So image object will work same as animation.
Thanks all for your answers.
In my opinion, I will separate this animation to 4 parts:
animation to show full black tree from center.
animation to show Texts
animation to show blue leafs.
animation to show 2 buttons at bottom.
You can use this framework https://github.com/facebook/pop to operate all animations step by step or even Core Animation.
Please do more research... I think you will success to make one.
You can achieve this animation using simple UIImageView, that can load multiple images using animationImages property of UIImageView.
First, create one NSMutablerArray of images.
then asssign that images to imageView, and animationDuration for that images.
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 193)];
animationImageView.animationImages = images;
animationImageView.animationDuration = 0.5;
add the imageview to view.
[self.view addSubview:animationImageView];
[animationImageView startAnimating];

iOS Image Animation

I want to show an image of glass getting filled with water in iPhone using iOS 8. I can get a white background image of glass (that shows empty glass) and water color could be light grey, for example.
Is there an option to fill background color of glass (in intervals)
so that it looks as if the glass is really getting filled with water?
Can I draw complete image (glass already filled with water) part by
part from bottom to top to show similar effect?
Any options to display/animate such an image?
With UIImageView you can do like that, create an images for each frame:
images = #[[UIImage imageNamed:#"img1.png"],
[UIImage imageNamed:#"img2.png"],
[UIImage imageNamed:#"img3.png"]];
[imageView setAnimationImages:testArray] ;
imageView.animationDuration = 0.5;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
Another way is to have UIView's/UIImageView's composition, e.g. UIImageView in front, use it as mask, another one in a back, and just use simple UIView animation.
But anyway, such a animation can be done in many ways, depends on your needs and how nice this animation should be.

IOS Button border thickness lssue,why?

UIButton top border appears thicker than the following ,but sometimes correct ,why?
code:
UIImage * sanImage = [UIimage imageNamed:#"product_bt1_normal"];
[self.saveBtn setBackgroundImage:[sanImage
stretchableImageWithLeftCapWidth:sanImage.size.width/3
topCapHeight:sanImage.size.height/3] forState:UIControlStateNormal];
Are you trying to make a button? If so, perhaps use a UIButton instead? You can control the border with button.layer.borderWidth = 1.0f
If you're set on using an image, create a UIImageView, and modify the border thickness that way:
UIImageView *iv = [[UIImageView alloc] initWithImage:sanImage];
[iv.layer setBorderWidth:0.5f];
It could be because of off-pixel boundaries. Since you are using height/3.0f, your image is maybe not returning a well-behaved image.
Also, there is a new stretchable image method you should be using, resizableImageWithCapInsets:.
So try this code out:
[self.saveBtn setBackgroundImage:[sanImage resizableImageWithCapInsets:UIEdgeInsetsMake(3.0f, 3.0f, 3.0f, 3.0f)] forState:UIControlStateNormal];
You might need to mess with the values for the insets a bit, I don't know the dimensions of your button image.

UIImage's resizableImageWithCapInsets places a shadow on image

I'm having trouble customising an UISegmentedControl: I've subclassed it I'm setting it's background for both the selected state and the unselected state like this:
#define kEdgeInsets UIEdgeInsetsMake(18, 18, 18, 18)
UIImage *grayImage = [[UIImage v_imageNamed:#"gray_rect"] resizableImageWithCapInsets:kEdgeInsets];
[self setBackgroundImage:grayImage
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
UIImage *greenImage = [[UIImage v_imageNamed:#"green_rect"] resizableImageWithCapInsets:kEdgeInsets];
[self setBackgroundImage:greenImage
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
[self setTintColor:[UIColor colorWithRed:0.506 green:0.514 blue:0.525 alpha:1.000]];
Where this are the PNGs I'm using
Now, when I execute this code, I get a weird shadow on the segmented control, which is not what we want. This is what the output looks like
Which is very weird, because there is no shadow on the original images, nor does UISegmentedControl add one (as far as I know).
Further checking, I noticed that if I removed the resizableImageWithCapInsets: call, the image looks distorted (as one should expect) but without the shadow.
Any ideas? because I'm literally going mad over this, since I don't have this problem when using resizableImageWithCapInsets: with UIButton
Thanks a lot!
I figured it out.
Turns out the segmented controller's frame is of 44pts height, and the background images was 75ptsx75pts. Since i've set the image's top and bottom inset to 18pts, the OS was taking the image's top and bottom 18pts and resizing it, ignoring the rest. Here is the fun part, since the image has a gradient, the rest of the image is ignored and it the control is colored like that.
In order to use images with vertical gradients you must use a base PNG with the exact same height as your control (http://useyourloaf.com/blog/2012/07/05/customizing-appearance-with-resizable-images.html)
This is what I did, resize the image to 44x44pts and change the insets to UIEdgeInsetsMake(0, 10, 0, 10) denoting that the image can't have to be resized vertically

Resources