Adding a Border to UIimage - iphone

I have a few questions on adding border to UIimage :
1) Is there anyway to add a dynamic border to an image of uiimageview?? I have tried to add border by using [layer setBorderColor:[UIColor whiteColor].CGColor]; But the border just shows around the frame of the uiimageview (a square or a rectangle). All my images are with thick black outlines and custom shape(such as hellokitty), I would like to add borders around the outlines. Is that possible to do it???
(I have also tried to add shadow around the images, but the result is too blurry, anyway to make them solid?? I think thats another way to solve my problem)
2) Also, if I can draw a custom shape border to stick around the images, is that possible to fill in color inside the border??, because some of my images have no filled color, any ideas??
Thanks in advance.

If your images are transparent with random shapes (i.e. the outline of the image is the shape of hello kitty) then I would approach this by...
First, ignore UIImageView here. Prob easier to create a custom UIView subclass.
In the drawRect of your view...
- (void)drawRect:(CGRect)rect
{
// calculate the rect of the image (aspect ratio etc...) so that it fits in the rect.
// see this link https://stackoverflow.com/questions/7645454/resize-uiimage-by-keeping-aspect-ratio-and-width
// change the colour of the image to black (or the border colour you want).
// see this link http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage
// draw the black image in the rect you calculated
// make a new rect that is smaller by in x and y than the one for the black image.
// You can change it by different amounts to change the border size
// draw the colour image in the new rect
}
Each step requires quite a bit of code so I've only put links and suggestions but this is what I'd do if I was trying to do what you are doing.
Link 1 - Resize UIImage by keeping Aspect ratio and width
Link 2 - http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage

Related

Set alpha on one color in UIImage

I have this PNG image: (the area in the grey borders is transparent)
I would like only the grey corners to have alpha of 0.5
If I set
[imgView setAlpha:0.5f];
Also the transparent area in the middle retrieves a semi-white color.
How can I do it?
Any reason why you don't just change the original image file? If that's not possible, you could put a white (black?) rectangle with 50% opacity below the image.
Of course there are many ways to do this but you would need to tell us more about your use case.
You should add one imageview below your desired image. and adjust alpha of that image.

Create a border around UIImageView using 8 images

I have 8 images I made, 4 images for each of the corners, and 4 images for each of the sides of UIImageView. Now, how on Earth do I make them 'come around' the view so they form a nice-looking, stretchable border, something like this (the white line is the border I try to implement):
Any ideas on how do you implement a border of a view based on 8 preset images?
EDIT: I don't want to draw the border, I want to use my 8 images!
It is not really what you are asking for but this is the solution closest to what you are trying to do.
You should take your 8 images and combine them into one single image and then make a resizable image out of that using resizableImageWithCapInsets:resizingMode:.
The first arguments lets you specify the top, left, bottom and right inset (i.e. the sizes of your images).
The second argument determines if the areas are stretched or tiled.
Stretched
Tiled
So to use it you would do something similar to
UIImage *myFullImage = [UIImage imageNamed:#"nameOfTheCombinedImage"];
UIImage *resizableImage =
[myFullImage resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)resizingMode:UIImageResizingModeTile];
(stretched vs tiles images "borrowed" from here)

Clear image with fade border

I have two UIImageView with same frame. One imageView above second imageView now I'm erasing upper imageView using Core Graphics. My problem is that the border of eraser is very sharp I want faded border i.e. after erasing the upper image should match the lower one's pallet. See below example -
I used these two images and below is result -
As you can see in this image eraser's border is very sharp. I want to make it faded out like in the middle it should be dark then light then more light and so on i.e. we cant see the width and end of brush. And here is my - My Sample Code Let me know if my question is not clear enough.
You can have a png image like brush (dark in the middle and light in the corner) and draw the image over your image like this.
[eraser drawAtPoint:location blendMode:kCGBlendModeDestinationOut alpha:1];

Quartz shadows not drawing correctly

So, I've got this image for my app that looks like this:
Now this is nice and all, but I'm trying to reduce my dependency on images and instead try to draw this dynamically.
Okay, so I found this image built into UIColor: (scrollViewTexturedBackgroundColor)
Looks kinda light, but sweet. We're getting there.
So now, I override drawRect in a UIView subclass to darken that a bit.
-(void)drawRect:(CGRect)rect {
[[UIColor colorWithWhite:0.0 alpha:0.4] setFill];
UIRectFillUsingBlendMode(rect , kCGBlendModeDarken);
}
Hey, it's not perfect but it's close enough. I can always tweak the blending mode & color later. Now comes the hard part. I can't figure out how to get the shadows to draw correctly. For example, I tried to set my shadow like this:
CGContextSetShadowWithColor(ctx, CGSizeMake(0, 6), 5, [[UIColor blackColor]CGColor]);
However, the shadow doesn't display. Also, I tried saving the graphics state, applying the shadow, then restoring it, but when I did that the shadow appeared but with the color in reverse (probably due to the blending mode I set). Obviously that's not what I'd hoped.
Any ideas?
The shadow is rendered with something that is drawn. You cannot draw just a shadow without actual object which drops it. If you find a way to do it, please let me know.
I see on the first picture you use inner shadow effect. To have a similar result you need to draw a path that drops a shadow around the area.
On the picture the red path is a bezier path with stroke of 10px. I have left a thing gap between the picture and the path to show that the path is bigger than the background picture.
To generate such a path you can use a bezier path with rect. Inset the the background picture rect by negative value that equals a half of the stroke width.
If the actual context is bigger then the picture, clip it with the picture rect before drawing the path.

How can I 'shine' a png on the iPhone in the same way that the app launcher does?

I want to show users what their square flat .png image will look like when converted to a normal 'shined' icon by the app launcher.
e.g. round corners and glassy effect.
Thanks
For in-application representation of the 'shine' on an icon, you can create a custom UIView that draws the shine gradient, using the code here (adjusting the gradient colors to match Apple's). When you want to apply the 'shine' to the preview icon, just overlay this custom UIView on top of its UIImageView (or whatever you're hosting it in).
The rectangular clipping could be a little trickier. If you have a solid black background, you could overlay a frame UIImageView that has a black area with a rounded rectangular transparent region in its center. You can also do this in a more general-purpose manner via Core Graphics by drawing your image and the gloss into a view, then using CGContextClip with a rounded rectangle to carve out the rounded interior.