2010-12-10 55 views
20

有人可以幫我嗎?新的iPhone開發人員。我試圖在一個圓圈內顯示.png圖片,而不是一個矩形,這是一個標準的iPhoneUIImage in a circle

+0

http://stackoverflow.com/a/21329347/501487 – aToz 2014-01-24 09:47:46

+0

http://stackoverflow.com/questions/7399343/making-a-uiimage-to-a-circle-form/21954535#21954535 – 2014-11-18 06:05:02

回答

9

使用UIImageView並將cornerRadius設置爲高度和寬度的一半。 view.layer.cornerRadius = cornerRadius;

UIImage rounded corners

51

好了,所有的PNG文件是「長方形」,但如果你想擁有你可以通過使用transparacy這樣做在屏幕上一個圓或其他非矩形對象的apperence。爲了確保圖像中的透明像素在iPhone上也是透明的,你可以設置UIImageView的背景顏色清除。這可以在Interface Builder的背景顏色選擇器拖動不透明度滑塊一路下跌來完成, 或代碼如下:

UIImage *image = [UIImage imageNamed:@"yourRoundImage.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
imageView.backgroundColor = [UIColor clearColor]; 
[self.view addSubview: imageView]; 

如果您只是想添加圓的角落,做一個圈,你也可以使用cornerRadius屬性這樣的,如果你已經添加了QuartzCore框架到您的項目:

#import <QuartzCore/QuartzCore.h> 
UIImage *image = [UIImage imageNamed:@"yourRoundImage.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
imageView.layer.cornerRadius = image.size.width/2; 
imageView.layer.masksToBounds = YES; 
[self.view addSubview: imageView]; 
+0

一個正常工作的解決方案!然而,如果你有很多圖像 – Vinzius 2014-01-16 19:22:35

+5

cornerRadius線應該是'imageView.layer.cornerRadius = imageView.frame.size.width/2;' – guptron 2014-12-12 02:09:50

+0

不工作的情況下ipad – 2015-07-22 08:18:23

24

試試這個代碼

yourImageView.layer.cornerRadius = yourImageView.frame.size.height /2; 
yourImageView.layer.masksToBounds = YES; 
yourImageView.layer.borderWidth = 0; 

這個展示圖像像ios 7圓圈圖像謝謝

+0

我試過這工作正常謝謝@ waseem – iOSDevp 2015-07-14 10:35:42

+0

圖片感謝;) – animaonline 2015-12-17 17:25:46

+0

完美!謝謝 – malhal 2016-01-12 17:05:27

2

如果在視圖中只有幾幅圖像,更改圖像視圖的角半徑效果很好。但是如果圖像視圖在tableview中,性能會受到影響。

一些其他的選項:

  1. 使在服務器上的圖像資產圓,或者如果它們手動被捆綁到應用程序,具有透明部分的圓的外區域。
  2. 如果圖像視圖的背景沒有改變,則創建一個覆蓋圖像,其中內圓部分是透明的,其餘部分與背景相同。還要將圖像視圖的backgroundColor設置爲clearColor。
  3. 接收圖像時,請在代碼中將其編輯爲後臺線程中的圓。
4

嘗試使用此方法獲得圖像查看的圓角和還以顏色角落:

self.imgView.layer.cornerRadius =self.imgView.frame.size.height/2; 
self.imgView.layer.masksToBounds = YES; 
self.imgView.layer.borderColor = [UIColor colorWithRed:148/255. green:79/255. blue:216/255. alpha:1.0].CGColor; 
self.imgView.layer.borderWidth=2; 

條件*:高度和ImageView的寬度必須與獲得圓角。

12

我與一個迅速擴展的貢獻用於設置的UIImageView爲圓

extension UIImageView{ 

    func asCircle(){ 
     self.layer.cornerRadius = self.frame.width/2; 
     self.layer.masksToBounds = true 
    } 

} 

只需撥打MyImageView.asCircle()

+2

是的,我已經在我的代碼中實現了完全相同的解決方案,除了我已經做了UIView的擴展,因爲即使對於其他對象(如按鈕...),我也可以調用相同的方法。 – Andrej 2016-02-17 17:07:20

0

我將添加一個稍微更普遍的擴展UIImageView將與非方形圖像工作。 要注意的是,它將比cornerRadius方法工作更慢。

extension UIImageView { 
    @IBInspectable public var asEllipse:Bool { 
     get { 
      if let mask = self.layer.mask { 
       return mask.name == kMaskLayerName 
      } 
      return false; 
     } 

     set { 
      if (newValue) { 
       let ellipseMask = CAShapeLayer() 
       ellipseMask.name = kMaskLayerName 
       ellipseMask.path = CGPathCreateWithEllipseInRect(self.bounds, nil) 
       ellipseMask.strokeColor = UIColor.clearColor().CGColor 
       ellipseMask.fillColor = UIColor.whiteColor().CGColor 
       self.layer.mask = ellipseMask 
      } else if self.asEllipse { 
       self.layer.mask = nil 
      } 
     } 
    } 

} 

private let kMaskLayerName="EllipseMaskLayer" 
+0

爲什麼這比'cornerRadius'慢? – 2016-03-01 20:43:27

+0

我猜想這是緩慢的對象分配:一個'CAShapeLayer',一個'CGPath'和兩個'UIColor'實例。儘管已經測試過iPhone 5上的tableView中的單元格圖像,但在滾動時沒有明顯的減速。 TBH我沒有對儀器進行適當的優化調查。 – user3099609 2016-03-02 14:06:14

0

斯威夫特4:這應該顯示了一圈你巴紐。

  1. 將圖像的IBOutlet拖動(按Ctrl +單擊)到您的代碼。

enter image description here

cornerRadius 半徑繪製圓角爲層的背景時使用。動畫。 https://developer.apple.com/documentation/quartzcore/calayer/1410818-cornerradius

clipsToBounds屬性 一個布爾值,確定子視圖是否被限制在視圖的邊界內。 https://developer.apple.com/documentation/uikit/uiview/1622415-clipstobounds

2. Inside viewDidLoad(),使用實例屬性layer.cornerRadiusclipsToBounds

profileImage.layer.cornerRadius = 50 
profileImage.clipsToBounds = true