有人可以幫我嗎?新的iPhone開發人員。我試圖在一個圓圈內顯示.png圖片,而不是一個矩形,這是一個標準的iPhoneUIImage in a circle
回答
使用UIImageView並將cornerRadius設置爲高度和寬度的一半。 view.layer.cornerRadius = cornerRadius;
好了,所有的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];
試試這個代碼
yourImageView.layer.cornerRadius = yourImageView.frame.size.height /2;
yourImageView.layer.masksToBounds = YES;
yourImageView.layer.borderWidth = 0;
這個展示圖像像ios 7圓圈圖像謝謝
我試過這工作正常謝謝@ waseem – iOSDevp 2015-07-14 10:35:42
圖片感謝;) – animaonline 2015-12-17 17:25:46
完美!謝謝 – malhal 2016-01-12 17:05:27
如果在視圖中只有幾幅圖像,更改圖像視圖的角半徑效果很好。但是如果圖像視圖在tableview中,性能會受到影響。
一些其他的選項:
- 使在服務器上的圖像資產圓,或者如果它們手動被捆綁到應用程序,具有透明部分的圓的外區域。
- 如果圖像視圖的背景沒有改變,則創建一個覆蓋圖像,其中內圓部分是透明的,其餘部分與背景相同。還要將圖像視圖的backgroundColor設置爲clearColor。
- 接收圖像時,請在代碼中將其編輯爲後臺線程中的圓。
嘗試使用此方法獲得圖像查看的圓角和還以顏色角落:
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的寬度必須與獲得圓角。
我與一個迅速擴展的貢獻用於設置的UIImageView爲圓
extension UIImageView{
func asCircle(){
self.layer.cornerRadius = self.frame.width/2;
self.layer.masksToBounds = true
}
}
只需撥打MyImageView.asCircle()
是的,我已經在我的代碼中實現了完全相同的解決方案,除了我已經做了UIView的擴展,因爲即使對於其他對象(如按鈕...),我也可以調用相同的方法。 – Andrej 2016-02-17 17:07:20
我將添加一個稍微更普遍的擴展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"
爲什麼這比'cornerRadius'慢? – 2016-03-01 20:43:27
我猜想這是緩慢的對象分配:一個'CAShapeLayer',一個'CGPath'和兩個'UIColor'實例。儘管已經測試過iPhone 5上的tableView中的單元格圖像,但在滾動時沒有明顯的減速。 TBH我沒有對儀器進行適當的優化調查。 – user3099609 2016-03-02 14:06:14
斯威夫特4:這應該顯示了一圈你巴紐。
- 將圖像的
IBOutlet
拖動(按Ctrl +單擊)到您的代碼。
cornerRadius 半徑繪製圓角爲層的背景時使用。動畫。 https://developer.apple.com/documentation/quartzcore/calayer/1410818-cornerradius
clipsToBounds屬性 一個布爾值,確定子視圖是否被限制在視圖的邊界內。 https://developer.apple.com/documentation/uikit/uiview/1622415-clipstobounds
2. Inside viewDidLoad(),使用實例屬性layer.cornerRadius
和clipsToBounds
。
profileImage.layer.cornerRadius = 50
profileImage.clipsToBounds = true
- 1. ImageViews in circle
- 2. 幫助Circle in C
- 3. boundingMapRect for a circle
- 4. VB Form In Circle Shape
- 5. Canvas Text in Center of concentric circle
- 6. Unhighlight a rich in a rich:ScrollableDataTable
- 7. Escape&in a string?
- 8. Endianess in a byte
- 9. Ruby in a helper
- 10. Slidingdrawer in a Fragment
- 11. javascript push in a loop
- 12. tinyMCE textarea in a tinyMCEPopup
- 13. TextToSpeech in a服務
- 14. SWT Tree in a ScrolledComposite
- 15. Undeclare a class in PHP
- 16. systemProperties in a external file?
- 17. MultiValueField in a optional inline
- 18. Oneliner -a-switch - in Perl
- 19. Eval ismember in a loop
- 20. Bootstrap type in a popover
- 21. ImageMagick Crop Circle from Circle?
- 22. 如何檢查列A IN(3,4,5)和A NOT IN(1,2)
- 23. import in in,得到「not a type」錯誤
- 24. 其中A IN()和IN() - 從其中A = 1和A = 2返回0行
- 25. Addpage wxAuiNotebook with a object in a vector
- 26. 如何匹配'<a href=' in a string?
- 27. UITableViewCell circle imageview
- 28. a + = a ++ * a ++ * a ++ in Java。它如何得到評估?
- 29. OpenGL C++ Circle vs Circle Collision
- 30. DACircularProgress Track Circle比Progress Circle大
http://stackoverflow.com/a/21329347/501487 – aToz 2014-01-24 09:47:46
http://stackoverflow.com/questions/7399343/making-a-uiimage-to-a-circle-form/21954535#21954535 – 2014-11-18 06:05:02