swi*_*ode 81 objective-c uitableview uiimageview ios
我的UIImageView每个UITableView单元格上都有一个显示远程图像(使用SDWebImage)的图像.我已经QuartzCore为图像视图做了一些图层样式,如下所示:
UIImageView *itemImageView = (UIImageView *)[cell viewWithTag:100];
itemImageView.layer.borderWidth = 1.0f;
itemImageView.layer.borderColor = [UIColor concreteColor].CGColor;
itemImageView.layer.masksToBounds = NO;
itemImageView.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
所以现在我有一个50x50的正方形,有一个微弱的灰色边框,但我想把它做成圆形而不是方形.该应用程序Hemoglobe在表视图中使用圆形图像,这就是我想要实现的效果.但是,我不想使用cornerRadius,因为这会降低我的滚动FPS.
这里Hemoglobe显示循环UIImageViews:

有没有办法达到这个效果?谢谢.
tag*_*bek 140
只需将cornerRadius宽度或高度设置为一半(假设对象的视图为方形).
例如,如果对象的视图的宽度和高度均为50:
itemImageView.layer.cornerRadius = 25;
Run Code Online (Sandbox Code Playgroud)
更新 - 正如用户atulkhatri指出的那样,如果你不添加它将无法工作:
itemImageView.layer.masksToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
yaz*_*azh 38
添加边框
self.ImageView.layer.borderWidth = 3.0f;
self.ImageView.layer.borderColor = [UIColor whiteColor].CGColor;
Run Code Online (Sandbox Code Playgroud)
对于圆形
self.ImageView.layer.cornerRadius = self.ImageView.frame.size.width / 2;
self.ImageView.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
请参阅此链接
http://www.appcoda.com/ios-programming-circular-image-calayer/
shi*_*vam 14
使用此代码..这将有所帮助..
UIImage* image = ...;
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
cornerRadius:50.0] addClip];
// Draw your image
[image drawInRect:imageView.bounds];
// Get the image, here setting the UIImageView image
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
这对我来说可以.:)
Ant*_*tzi 10
这是一个更新的方法,在swift中使用IBDesignable和IBInspectable来子类化UIImageView
@IBDesignable class RoundableUIImageView: UIImageView {
private var _round = false
@IBInspectable var round: Bool {
set {
_round = newValue
makeRound()
}
get {
return self._round
}
}
override internal var frame: CGRect {
set {
super.frame = newValue
makeRound()
}
get {
return super.frame
}
}
private func makeRound() {
if self.round == true {
self.clipsToBounds = true
self.layer.cornerRadius = (self.frame.width + self.frame.height) / 4
} else {
self.layer.cornerRadius = 0
}
}
override func layoutSubviews() {
makeRound()
}
}
Run Code Online (Sandbox Code Playgroud)
是的,这可能给layer.cornerRadius (需要添加
#import <QuartzCore/QuartzCore.h>)
为创建循环的任何控制,但在你的情况,而不是一组layer的UIImageView是创建自己的图像为圆形,并添加它的最佳方式UIImageView而曾经backGroundColor是ClearColor.
另请参阅这两个代码来源.
https://www.cocoacontrols.com/controls/circleview
和
https://www.cocoacontrols.com/controls/mhlazytableimages
这可能对您的情况有所帮助:
| 归档时间: |
|
| 查看次数: |
64549 次 |
| 最近记录: |