將影象製成圓形或圓形
這個例子說明了如何製作一個 UIView
或 UIImageView
,用這樣的半徑舍入:
https://i.stack.imgur.com/QRdJe.jpg
Objective-C
someImageView.layer.cornerRadius = CGRectGetHeight(someImageView.frame) / 2;
someImageView.clipsToBounds = YES;
迅速
someImageView.layer.cornerRadius = someImageView.frame.height/2
// this should alleviate the performance hit that adding transparency may cause - see http://stackoverflow.com/a/6254531/189804
// Be sure to check scrolling performance with Instruments if you take this approach.
someImageView.layer.shouldRasterize = true
someImageView.clipsToBounds = true // All parts of the image that are outside its bounds (the frame) are cut out (makes the rounded corners visible)
建議如果你使用自動佈局,你將 someImageView.layer.cornerRadius
程式碼放在 viewDidLayoutSubviews
中。如果影象改變大小,這將允許影象的 cornerRadius
更新。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
someImageView.layer.cornerRadius = someImageView.frame.size.width/2
someImageView.layer.masksToBounds = true
}