2017-06-26 65 views
0

我目前有下面的圖片,我試圖設置一個邊框。它由一個UIImageView的裏面(一transparent.pngObjective-C:在UIImage上繪製邊框

當我嘗試設置邊框爲我的圖像(見代碼),它提供了邊界到UIImage的圖像,但它不會「卡」在我圖片。是否有可能實現這種效果?

See image current implementation here.

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

看看這個:https://stackoverflow.com/questions/1354811/how-can-i-take-an-uiimage-and-give-it-a-black-border ... A很多都與普通方塊有關,但在底部有一個關於圍繞有趣(意思是不是方塊/圓圈等)圖像/形狀創建邊框的答案 – Burnie777

+0

你是如何創建/獲取該圖像的?你自己畫了嗎? – Larme

+0

@Larme不,這是我從資產 – iCappsCVA

回答

0

嘗試加入後面的UIImageView和一層邊框添加到它會做的伎倆

#define kBorderWidth 3.0. 
#define kCornerRadius 8.0 
CALayer *borderLayer = [CALayer layer]; 
CGRect borderFrame = CGRectMake(0, 0, (imageView.frame.size.width), (imageView.frame.size.height)); 
[borderLayer setBackgroundColor:[[UIColor clearColor] CGColor]]; 
[borderLayer setFrame:borderFrame]; 
[borderLayer setCornerRadius:kCornerRadius]; 
[borderLayer setBorderWidth:kBorderWidth]; 
[borderLayer setBorderColor:[[UIColor redColor] CGColor]]; 
[imageView.layer addSublayer:borderLayer]; 

而且不要忘記導入QuartzCore/QuartzCore.h

本示例將在圖層上繪製邊框,但稍微改變邊框以在圖層周圍創建邊框。根據您的需求

+0

請檢查我使用的圖像。這只是增加了一個cornerradius到UIImageView,而不是圖像本身 – iCappsCVA

0

,如果你不希望它是儘可能準確,快速和骯髒的解決辦法是這樣的:

- (UIImage *)borderedImageFromImage:(UIImage *)source andColor:(UIColor *)borderColor{ 
    CGFloat scale = 0.95;//this determines how big the border will be, the smaller it is the bigger the border 
    UIImage *borderImage = [source imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
    UIGraphicsBeginImageContextWithOptions(source.size, NO, source.scale); 
    [borderColor set]; 
    [borderImage drawInRect:CGRectMake(0, 0, source.size.width, source.size.height)]; 
    [source drawInRect:CGRectMake(source.size.width*(1-scale)/2, 
            source.size.height*(1-scale)/2, 
            source.size.width * scale, 
            source.size.height * scale)]; 
    borderImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return borderImage; 
} 

,這裏是如何使用它:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.heartImageView.image = [self borderedImageFromImage:[UIImage imageNamed:@"heart"] andColor:[UIColor blackColor]]; 

} 

這實質上是做的是繪製你想要的圖像兩次,一次在邊界的顏色(略微縮放),一次使用正常的顏色。您的里程可能會因圖像而異。

+0

如何將它與原來一起使用?做一個setImage覆蓋它,但使用你自己的函數本身不做任何事情 – iCappsCVA

+0

'UIImageview * imageview.image = [self borderedImageFromImage:source andColor:[UIColor blackColor]];'' – Aris