Stretchable UIImage chat bubbles - ios

I have the following image attached that I use as the background of each of my chat messages (i.e. bubbles). What values should I use for X and Y in UIImage stretchableImageWithLeftCapWidth: X topCapHeight: Y]; to insure that the pointy end of the image stays in the middle at all times.
I tried using X=20 Y=5 but the pointy end goes to the bottom.

You can't do that with a single stretchable image. A stretchable image always has just one rectangular area that is stretched. To get the appearance you want however, you'd need to stretch two parts – the areas above and below the pointer.
You need to use at least two images. The easiest would probably be to make the pointer a separate image and center it manually on top of the rounded rectangle.

You can't do that and keep the arrow in the center -- well, not in iOS 6 and earlier ;-)
This is why the little speech arrow in Messages.app is at the bottom corner of the speech bubbles. They stretch an area of pixels that is just above that region.
So, you could make things easier for yourself by following Messages.app's lead and design around the limitation.

Related

iOS 'colorWithPatternImage' is adding 1 px borders around my images?

I'm using UIColor's colorWithPatternImage function to set a tiled image on one of my views. The result is a grid of 1 pixel lines all over.
Fig: The clear color grid of lines is the issue.
My intention was to obtain a perfect background using the tiled image.
I first suspected that the image I was using could be faulty, but zooming it to 800% doesn't really show the presence of any transparent one-pixel border anywhere.
Here's the image (#2x version):
Any ideas what it could be related to?
Thanks,
p.
you are doing everything fine, but your problem is that your pattern image have 1 pixel line on the top an 1 pixel line on the left side with alpha color so you only need to modify your pattern image simply as that, I have been testing and this is the problem
I hope this helps you

What is use of stretching property of UIImageView. How I can use this property.?

I want to know how can i use this property for UIImageView.
Stretching properties are pretty simple, as stated by Karol Kozub in this article:
The fraction of the original image left without stretching on the left
is specified by X
The fraction of the original image that gets stretched in the x-axis
is specified by Width
The fraction of the original image left without stretching on the
right is equal to 1 – X – Width
If we use 0 for Width the stretched area will interpolate between
the last pixel of the left part and the first pixel of the right part
The y-axis works analogously
This sets the contentStretch property for views (this is a UIView property, not specifically a UIImageView property). This property has been deprecated since iOS 6, however, so you shouldn't use it.
The replacement, specifically for images, is resizableImageWithCapInsets. The normal use of this is to create an image with a left and right side (or top and bottom), and a single-pixel wide "middle" that is stretched across the view. It's common for custom buttons both because it's flexible to a variety of widths, and because it saves some space.
See "Defining a Stretchable Image" in the UIImage docs for full details.

How to slicing image and make it stretchable with image assets

stretch image right and left side
centre arrow remain as it is
i tried with it but centre down arrow position is not properly set by me.
You can't do this with sliced images.
The area between the edges will stretch or repeat in order to fill the area. You can't also preserver the centre section of an image this way.
What you might be better doing is creating a custom drawn view using either CALayer or drawRect.

Xcode 5 Image Slicing

I'm trying to slice an image in assets Xcode 5. I don't know if what I'm trying to do is possible with slicing.
The image looks like this:
I need to change the width and the height of the image, although not at the same time. The closest I've got is when I use Horizontal for the height and Vertical for the width. The problem is when I change the width, the left side of the image starts as a straight line and the skews and change to the original shape.
I'm about to give up on the idea of slicing and just create three images, left, center and right. Is that the only way to go?
This is how my current slice looks like:
Is there any way to change it so it works at least for the width?
UPDATE
Instead of slicing the image I used a mask and moved the mask.
Slicing works by taking the center section, and stretching it to fill the required space and then putting on the image end caps.
Because your button has a continuous diagonal line you will not be able to slice it. It will stretch the image out like so:
In fact even if you make a centre section and end caps you will still have the same issue as this image can only be scaled.
If you only need a finite set of heights then you should create individual assets for each height that can be sliced horizontally. If you need N heights then you should look into using core graphics to draw your shape in code in a -drawRect: method for arbitrary dimensions.
P.S. if you want to horizontally slice so you can grow the width its most optimal to have only 1 pixel width of image that will be stretched out so your button image resources would look a bit like this:
Aha. TIL: You can change the slicing options from the attributes inspector in the right hand pane in Xcode. To make your slicing work choose only "Horizontal"

Is it possible to resize an image but keep the borders the same on iOS?

I have an image with a fixed border, and I need to be able to change the height of the image from for example 25px to 300px but so that the border on top only makes up the 5px it did originally. Of course the possibility is to keep as many version of the same image in different sizes, as many sizes I have, but that would take up huge amounts of memory, or I could try to rather make up one image of 5 parts, that is upper border, bottom border, left and right borders, and the actual content. This is, however, rather inconvenient
Thanks
You are looking for the UIImage method
-(UIImage *)resizableImageWithCapInsets:
Send this message to your original image and pass UIEdgeInsetsMake(topFixedBorderSize, leftFixedBorderSize, bottomFixedBorderSize, rightFixedBorderSize) and you'll get back a "resizable" image. When you draw the resizable image at a larger size only the pixels not covered by the cap insets will be stretched. The pixels covered by the cap insets on every side will remain fixed on the edge.

Resources