ios UITableView 中的圆形 UIImageView 没有性能影响?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17721934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Circular UIImageView in UITableView without performance hit?
提问by swiftcode
I have a UIImageView
on each of my UITableView
cells, that display a remote image (using SDWebImage
). I've done some QuartzCore
layer styling to the image view, as such:
我的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;
So now I have a 50x50 square with a faint grey border, but I'd like to make it circular instead of squared. The app Hemoglobe
uses circular images in table views, and that's the effect I'd like to achieve. However, I don't want to use cornerRadius
, as that degrades my scrolling FPS.
所以现在我有一个带有微弱灰色边框的 50x50 正方形,但我想让它变成圆形而不是方形。该应用程序Hemoglobe
在表格视图中使用圆形图像,这就是我想要达到的效果。但是,我不想使用cornerRadius
,因为这会降低我的滚动 FPS。
Here's Hemoglobe
showing circular UIImageViews
:
这是Hemoglobe
显示圆形UIImageViews
:
Is there any way to get this effect? Thanks.
有什么办法可以达到这个效果吗?谢谢。
回答by tagabek
Simply set the cornerRadius
to half of the width or height (assuming your object's view is square).
只需将 设置为cornerRadius
宽度或高度的一半(假设您的对象视图是正方形)。
For example, if your object's view's width and height are both 50:
例如,如果您的对象视图的宽度和高度均为 50:
itemImageView.layer.cornerRadius = 25;
Update - As user atulkhatri points out, it won't work if you don't add:
更新 - 正如用户 atulkhatri 指出的那样,如果您不添加,它将无法工作:
itemImageView.layer.masksToBounds = YES;
回答by yazh
To Add border
添加边框
self.ImageView.layer.borderWidth = 3.0f;
self.ImageView.layer.borderColor = [UIColor whiteColor].CGColor;
For Circular Shape
对于圆形
self.ImageView.layer.cornerRadius = self.ImageView.frame.size.width / 2;
self.ImageView.clipsToBounds = YES;
Refer this link
参考这个链接
http://www.appcoda.com/ios-programming-circular-image-calayer/
http://www.appcoda.com/ios-programming-circular-image-calayer/
回答by shivam
Use this code.. This will be helpful..
使用此代码.. 这将是有帮助的..
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();
It works fine for me. :)
这对我来说可以。:)
回答by Antzi
Here is a more up to date method in swift using IBDesignable and IBInspectable to subclass UIImageView
这是使用 IBDesignable 和 IBInspectable 对 UIImageView 进行子类化的 swift 更新方法
@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()
}
}
回答by iPatel
Yes, it is possible to give layer.cornerRadius
(need to add
#import <QuartzCore/QuartzCore.h>
)
for create circular any control but in your case instead of set layer
of UIImageView
it is The
Best Way to Create Your Image as circular and add it on UIImageView
Which have backGroundColor
is ClearColor
.
是的,这可能给layer.cornerRadius
(需加
#import <QuartzCore/QuartzCore.h>
)
为创建循环的任何控制,但在你的情况,而不是一套layer
的UIImageView
它是最好的方法制作图片为圆形,并将其添加在UIImageView
其中有backGroundColor
是ClearColor
。
Also refer this Two Source of code.
另请参阅此代码的两个源。
https://www.cocoacontrols.com/controls/circleview
https://www.cocoacontrols.com/controls/circleview
and
和
https://www.cocoacontrols.com/controls/mhlazytableimages
https://www.cocoacontrols.com/controls/mhlazytableimages
This might be helpful in your case:
这可能对您的情况有所帮助:
回答by Amritansh Upadhyay
Set the height & width of the of the UIImageView to be the same e.g.:Height=60
& Width = 60
, then the cornerRadius
should be exactly the half.I have kept it 30 in my case.It worked for me.
将 UIImageView 的高度和宽度设置为相同,例如:Height=60
& Width = 60
,那么cornerRadius
应该正好是一半。在我的情况下,我将它保持为 30。它对我有用。
self.imageView.layer.cornerRadius = 30;
self.imageView.layer.borderWidth = 3;
self.imageView.layer.borderColor = [UIColor whiteColor].CGColor;
self.imageView.layer.masksToBounds = YES;
回答by Zaporozhchenko Oleksandr
If use some autolayout and different cells heights, u better do it this way:
如果使用一些自动布局和不同的单元格高度,你最好这样做:
override func layoutSubviews() {
super.layoutSubviews()
logo.layer.cornerRadius = logo.frame.size.height / 2;
logo.clipsToBounds = true;
}
回答by Hossam Ghareeb
Usable solution in Swift
using extension
:
在可用的解决方案Swift
使用extension
:
extension UIView{
func circleMe(){
let radius = CGRectGetWidth(self.bounds) / 2
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
}
}
Usage:
用法:
self.venueImageView.circleMe()
回答by Moose
I use a Round Image View class… So I just use it instead of UIImageView, and have nothing to tweak…
我使用了一个圆形图像视图类……所以我只是用它来代替 UIImageView,没有什么可调整的……
This class also draws an optional border around the circle. There is often a border around rounded pictures.
这个类还在圆周围绘制了一个可选的边框。圆形图片周围通常有边框。
It is not a UIImageView subclass because UIImageView has its own rendering mechanism, and does not call the drawRect method..
它不是 UIImageView 子类,因为 UIImageView 有自己的渲染机制,并且不会调用 drawRect 方法。
Interface :
界面 :
#import <UIKit/UIKit.h>
@interface MFRoundImageView : UIView
@property(nonatomic,strong) UIImage* image;
@property(nonatomic,strong) UIColor* strokeColor;
@property(nonatomic,assign) CGFloat strokeWidth;
@end
Implementation :
执行 :
#import "MFRoundImageView.h"
@implementation MFRoundImageView
-(void)setImage:(UIImage *)image
{
_image = image;
[self setNeedsDisplay];
}
-(void)setStrokeColor:(UIColor *)strokeColor
{
_strokeColor = strokeColor;
[self setNeedsDisplay];
}
-(void)setStrokeWidth:(CGFloat)strokeWidth
{
_strokeWidth = strokeWidth;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGPathRef path = CGPathCreateWithEllipseInRect(self.bounds, NULL);
CGContextAddPath(ctx, path);
CGContextClip(ctx);
[self.image drawInRect:rect];
if ( ( _strokeWidth > 0.0f ) && _strokeColor ) {
CGContextSetLineWidth(ctx, _strokeWidth*2); // Half border is clipped
[_strokeColor setStroke];
CGContextAddPath(ctx, path);
CGContextStrokePath(ctx);
}
CGPathRelease(path);
}
@end
回答by lifeisfoo
In swift, inside your viewDidLoad
method, with the userImage
outlet:
在 swift 中,在您的viewDidLoad
方法中,带有userImage
出口:
self.userImage.layer.borderWidth = 1;
self.userImage.layer.borderColor = UIColor.whiteColor().CGColor;
self.userImage.layer.cornerRadius = self.userImage.frame.size.width / 2;
self.userImage.clipsToBounds = true;