我想做一个UITableView,以便在每个单元格中都可以看到像联系人中个人资料图片一样的图像。

我制作了一个UIView来制作自定义图纸,在draw rect中,它在iPhone 5c中可以正常工作
但是当我滚动表格视图时,它会杀死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;
}


我在此Graphics上下文中不好
也许我做错了:(

有什么办法可以加快这个过程?

最佳答案

首先将其导入您的班级#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];

关于ios - UIImage具有圆形边框,如联系人中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25973897/

10-13 03:50