2014-05-14 29 views
2

我有一個UIImageView。我想使它成爲一個圓形類型和5像素的紅色邊框。如何使UIImageView成爲一個圓圈[iOS]

怎麼可能?我也分享代碼和圖像。請有人幫助我嗎?

enter image description here

我的代碼是:

UIImageView *myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(130, 200, 60, 60)]; 
[myImageview setImage:[UIImage imageNamed:@"image.png"]]; 
[self.view addSubview:myImageview]; 
+0

的可能重複的[IOS:創建一個UIImage或UIImageView的具有圓角(http://stackoverflow.com/questions/7705879/ios-create-a-uiimage-or-uiimageview-with-rounded-corners) – n00bProgrammer

回答

6

只要你能做到。

只要按照這個代碼..

#import <QuartzCore/QuartzCore.h> 

[myImageview.layer setBorderColor:[[UIColor redColor] CGColor]]; // For border color 
[myImageview.layer setBorderWidth:5.0]; // For Border width 
[myImageview.layer setCornerRadius:45.0f]; // For Corner radious 
[myImageview.layer setMasksToBounds:YES]; 
+0

非常感謝。 – user3631436

4

試試這個對的UIImageView:

myImageview.layer.cornerRadius = myImageview.width/2; 
myImageview.layer.masksToBounds = YES; 
myImageview.layer.borderWidth = 5.0f; 
myImageview.layer.borderColor = [[UIColor redColor] CGColor]; 

myImageview.contentMode = UIViewContentModeCenter; 
0

進口QuartzCore框架 然後在.h文件中

#import <QuartzCore/QuartzCore.h> 

然後,只需做以下

UIImageView *myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(130, 200, 60, 60)]; 
[myImageview setImage:[UIImage imageNamed:@"image.png"]]; 
myImageview.layer.cornerRadius = 30; 
myImageview.layer.borderWidth = 5.0; // or whatever width you want to apply 
myImageview.layer.borderColor = [[UIColor redColor] CGColor]; 
[self.view addSubview:myImageview]; 
1

確保你有你的項目QuartzCore框架,然後在其中創建此圖像查看文件進口#import <QuartzCore/QuartzCore.h>

UIImageView *myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(130, 200, 60, 60)]; 
[myImageview setImage:[UIImage imageNamed:@"image.png"]]; 

myImageview.clipsToBounds = YES: 
myImageview.layer.masksToBounds = YES; 
myImageview.layer.cornerRadius = myImageview.bounds.width/2.0f; 
myImageview.layer.borderWidth = 5.0; 
myImageview.layer.borderColor = [UIColor redColor]; 


[self.view addSubview:myImageview]; 
0

需要到您正在創建圖像視圖在文件中添加QuartzCore框架在您的項目,然後#import<QuartzCore/QuartzCore. h>

UIImageView *myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(130, 200, 60, 60)]; 
[myImageview setImage:[UIImage imageNamed:@"image.png"]]; 
myImageview.clipsToBounds = YES: 
[self.view addSubview:myImageview]; 
[self updateImageViewLayer:myImageview.layer withBorderColor:[UIColor redColor] borderWidth:5.f cornerRadius:(myImageview.bounds.width/2.f)]; 

- (void)updateImageViewLayer:(CALayer*)layer withBorderColor:(UIColor *)color borderWidth:(CGFloat)width cornerRadius:(CGFloat)radius 
{ 
    layer.borderWidth = width; 
    layer.borderColor = color.CGColor; 
    layer.cornerRadius = radius/2.f; 
    layer.masksToBounds = YES; 
} 

我做了一個函數,重用它用於不同的UIImageView對象。

0

夫特3解決方案:

extension UIImageView{ 
    var circled : UIImageView{ 
     self.layer.cornerRadius = self.frame.width/2; 
     self.layer.borderWidth = 2 
     self.layer.borderColor = UIColor.red.cgColor 
     self.clipsToBounds = true 
     return self 
    } 
} 

用法:

imageView?.circled.image = UIImage()