Iphone,如何在图像上添加轮廓?

时间:2011-06-11 15:10:31

标签: iphone

我想在图片上添加大纲,你有什么想法吗? 注意:我不需要imageview的边框,不需要imageview.layer.borderColor,也不需要image.layer.borderWidth;

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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框架 这将为图像提供“1”的边框。