2012-12-13 104 views
2

我有几个关于向UIimage添加边框的问题:向UIimage添加边框

1)是否有将动态边框添加到uiimageview的图像?我试图通过使用[layer setBorderColor:[UIColor whiteColor].CGColor];来添加边框。但边框仅显示在uiimageview(一个正方形或矩形)的框架周围。我的所有图像都有粗黑的轮廓和自定义形状(比如hellokitty),我想在轮廓周围添加边框。这是可能的吗?

(我也尝试添加图像周围的影子,但结果却是太模糊了,反正让他们实?我认为是另一种方式来解决我的问题)

2)另外,如果我可以画一个自定义的形状边框来粘贴图像,是否可以在边框内填充颜色??因为我的一些图像没有填充颜色,任何想法??

在此先感谢。

回答

0

如果您的图像是透明的随机的形状(即图像的轮廓是凯蒂猫的形状),那么我会接近这个由...

首先,这里忽略的UIImageView。 Prob更容易创建一个自定义的UIView子类。

在您看来的的drawRect ......

- (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 
} 

每一步都需要相当多的代码,所以我只把链接和建议,但这是,如果我想我会做什么做你正在做的事情。

链接1 - Resize UIImage by keeping Aspect ratio and width

链接2 - http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage

+0

感谢您的回复,我Quatz2D的初学者,我会问我的想法是好还是不好: – user1901664

+0

忘记上述评论。谢谢对于你的回复,我是Quatz2D的初学者,作为你提供的link1,uiimage可以调用drawRect:为了在给定矩形(区域)内重绘整个图像,是否可以重写图像的drawRect函数然后修改笔画宽度? – user1901664