我想制作一个UITableView
,在每个单元格中,我都可以看到联系人资料图片中的图像。
我制作了一个UIView
制作自定义绘图,在draw rect中,它在iPhone 5c中运行FINE
但是当我滚动表格视图时它会杀死iPhone 4和iPhone4s
这就是我所做的:
// _image is iVar
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *patternRed = [UIColor colorWithPatternImage:[self imageWithImage:_image scaledToSize:CGSizeMake(self.frame.size.width,self.frame.size.height)]];//recale image to self bounds
CGContextSetLineWidth(context, 0.0);
CGContextSetFillColorWithColor(context, patternRed.CGColor);
CGContextAddEllipseInRect(context, rect);
CGContextDrawPath(context, kCGPathFill); // Or kCGPathFill //kCGPathFillStroke
}
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize
{
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
我在这个图形上下文中并不擅长 也许我做错了什么:(
有没有办法加快这个过程?
答案 0 :(得分:1)
首先将其导入您的班级#import <QuartzCore/QuartzCore.h>
然后将以下方法添加到您的类
-(UIImageView * )createRoundedImageview:(UIImageView*)imageView
{
// this works in the case of width and height of image view are same
imageView.layer.cornerRadius = imageView.frame.size.width/2.0; //or imageView.frame.size.height/2.0
imageView.layer.masksToBounds = YES;
return imageView;
}
然后调用你想要的圆形图像,如...
UIImageView * yourImageView = [[UIImageView alloc]initWithframe:CGRectMake(100,150,200,200)];
yourImageView.image = [UIImage ImageNamed:@"yourimageName"];
yourImageView = [self createRoundedImageview: yourImageView];
[self.view addSubView: yourImageView];
答案 1 :(得分:0)
button.clipsToBounds = YES;
button.layer.cornerRadius = 2.0f
button.layer.borderColor=[UIColor <colorname>].CGColor;
button.layer.borderWidth=2.0f; //
以上代码适合您
答案 2 :(得分:0)
你不需要像这样自己画画;您可以使用标准UIImageView
并设置其cornerRadius
以使其具有圆形效果(将其设置为edgeLength / 2
将创建一个完美的圆圈。)
答案 3 :(得分:0)
您可以使用以下代码创建圆角边框。
newImage.layer.cornerRadius = newImage.frame.size.width / 2.0;
newImage.clipsToBounds = YES;
newImage.layer.borderWidth = 2.0f;
使用imageview的cornerRadius属性的代码,它将半径等于图像的宽度2.0。 下一个语句允许您剪辑图像。 第三步是可选的。