开发者

Iphone, how to add outline on an image?

I want to add outline on an image, do you have any idea? Note: I don't need the 开发者_开发知识库border of imageview, not imageview.layer.borderColor nor image.layer.borderWidth;


Try this :

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

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


This may not be a very clean way but may work and is simple.

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

The sizes of the view and imageView will what give your image a border. eg. (0, 0, 10, 10) -> view frame (1, 1, 8, 8) -> imageView frame This will give border of "1" to the image.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜