2011-06-11 157 views

回答

5

试试这个:

- (UIImage*)imageWithBorderFromImage:(UIImage*)source; 
{ 
    CGSize size = [source size]; 
    UIGraphicsBeginImageContext(size); 
    CGRect rect = CGRectMake(0, 0, size.width, size.height); 
    [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(context, 1.0, 0.5, 1.0, 1.0); 
    CGContextStrokeRect(context, rect); 
    UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return testImg; 
} 

来源:http://www.icodesnip.com/snippet/objective-c/add-image-border-to-uiimage

+0

感谢您的建议,但你添加一个矩形边框,而我需要的是图像的轮廓。例如帽子图像,我需要围绕帽子的轮廓,而不是围绕它的矩形。 – 2011-06-12 09:25:41

+0

你有什么其他建议吗? – 2011-06-12 09:26:35

+0

这种操作可能需要图像处理,请尝试使用http://code.google.com/p/simple-iphone-image-processing/但它似乎不是那么简单 – AmineG 2011-06-12 11:36:56

1

这可能不是一个非常干净的方式,但可能工作和简单。

//Make a UIView instance with required coords and size 
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(...)]; 

//Set a backgroundColor, that will be the color of your border 
view.backgroundColor = [UIColor ...]; 

// Make a UIImageView instance with the frame just leaving enough 
// space around for the border. 
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(...)]; 
imageView.image = yourImage; 

// Add the imageView to the view. 
[view addSubview:imageView]; 
[imageView release]; 

// Release view accordingly after adding to some other view. 

视图和imageView的大小将使您的图像的边界。 例如。 (0,0,10,10) - >查看帧 (1,1,8,8) - > imageView frame 这将使图像的边界为“1”。