UITableViewCell圆形图像查看
我正在试图制作一个应用程序,该图像具有圆润的imageView中的图像,但图像的大小不同。我的目标是如果图像太大(因此图像看起来不扭曲),会出现一小块图像。UITableViewCell圆形图像查看
我能够得到圆润的imageView,但它总是不同的大小为不同的图像 - 这不是我想要的。我在这里看了帖子:
Using cornerRadius on a UIImageView in a UITableViewCell
我的一些代码是在这里的定义UITableViewCell(我子类):
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
// [self.imageView setContentMode:UIViewContentModeScaleAspectFill];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.bounds = CGRectMake(0, 0, 44,44);
self.imageView.frame = CGRectMake(5, 10, 44, 44);
CGRect frame = self.imageView.frame;
self.imageView.layer.cornerRadius = 15;
[self.imageView.layer setMasksToBounds:YES];
self.imageView.clipsToBounds = YES;
}
return self;
}
我使用SDWebImage
加载图片。还有什么我在这里失踪?
我找到了解决我的问题。
一些奇怪的原因......我是不是能够在UITableViewCell
使用可变图像的imageView
。最后我把一个占位符图像在imageView
(透明PNG),然后我把我自己的UIImageView
具有相同的代码在我的问题和它工作得很好。计算imageView
的方式正在做一些奇怪的事情。
如果你使角半径的数字与宽度或高度成正比,那么这会给你一个恒定的图像圆度。作为一个例子,我在这里建议div为3。
self.imageview.layer.cornerRadius = self.imageview.frame.size.width/3;
希望这是你要找的。
干杯,吉姆
编辑 -
我看你注释掉aspectFill,虽然这跟clipToBounds,或MaskToBounds应该提供您要查找的内容。所以通过改变
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
更改为
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
,并保持self.image.clipToBounds线,这应该是它。
干杯
似乎没有工作。我的问题是,对于图像尺寸的不同,我希望它不会扭曲图像视图,而且它总是这样做。 – KVISH
你好,这是最好的解决方案,非常快:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
CALayer * l = [self layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
// You can even add a border
[l setBorderWidth:self.frame.size.width/IMG_PROFILE_BORDER_SCALE];
[l setBorderColor:[[UIColor grayColor] CGColor]];
}
return self;
}
而且很明显,不要忘记导入QuartzCore框架:
#import <QuartzCore/QuartzCore.h>
但是,如果你有兴趣,我还在GitHub上创建了一个可用于创建图像周围的路径的对象。该解决方案使用CoreGraphics中,我认为它比上面的另一个慢:
UIViewContentModeScaleAspectFill
是更适合您的需求,因为它缩放图像填补的ImageView的大小,而不改变方位比例。
但不要忘记设置clipsToBounds
到YES
为ImageView的,以去除剩余的部分缩小视图界:
[self.imageView setClipsToBounds:YES];
可能重复的[iOS边角的iOS圆角UIImage](http://stackoverflow.com/questions/17959931/ios-rounded-corner-uiimage-with-border) – Pavan