如果您的图像是透明的,形状随机(即图像的轮廓是 hello kitty 的形状),那么我会通过...
首先,这里忽略 UIImageView。可能更容易创建自定义 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 -通过保持纵横比和宽度调整 UIImage 的大小
链接 2 - http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage