2
votes

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.

1

1 Answers

0
votes

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://stackguides.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