gpt4 book ai didi

ios - 如何使 imageView 固定大小并带有圆角

转载 作者:行者123 更新时间:2023-11-28 21:54:03 24 4
gpt4 key购买 nike

我正在使用 Parse.com 构建一个简单的应用程序。我想知道是否有任何方法可以使 imageView 大小固定(例如 30x30px)并使图像的角变圆?

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
PFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

PFUser *user = users[indexPath.row];
cell.textLabel.text = user[PF_USER_FULLNAME];

PFImageView *imageView = [[PFImageView alloc] init];
[imageView setBackgroundColor:[UIColor clearColor]];
cell.imageView.image = [UIImage imageNamed:@"tab_profile.png"]; // placeholder image

cell.imageView.file = (PFFile *)user[PF_USER_THUMBNAIL]; // remote image

[cell.imageView loadInBackground];

return cell;
}

请提供任何建议,我是 Xcode 和 Parse SDK 的新手...

1) enter image description here

2) enter image description here

最佳答案

这是我通常用来在 imageView 上获得圆圈的方法:

  1. 裁剪(而不是调整大小)图像以使其成为正方形。下面的方法将图像的大小调整为您作为 CGSize 传递的任何大小:

    (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize
    {
    double ratio;
    double delta;
    CGPoint offset;

    //make a new square size, that is the resized imaged width
    CGSize sz = CGSizeMake(newSize.width, newSize.width);

    //figure out if the picture is landscape or portrait, then
    //calculate scale factor and offset
    if (image.size.width > image.size.height)
    {
    ratio = newSize.width / image.size.width;
    delta = (ratio*image.size.width - ratio*image.size.height);
    offset = CGPointMake(delta/2, 0);
    } else {
    ratio = newSize.width / image.size.height;
    delta = (ratio*image.size.height - ratio*image.size.width);
    offset = CGPointMake(0, delta/2);
    }

    //make the final clipping rect based on the calculated values
    CGRect clipRect = CGRectMake(-offset.x, -offset.y,
    (ratio * image.size.width) + delta,
    (ratio * image.size.height) + delta);


    //start a new context, with scale factor 0.0 so retina displays get
    //high quality image
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
    } else {
    UIGraphicsBeginImageContext(sz);
    }
    UIRectClip(clipRect);
    [image drawInRect:clipRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
    }

然后,调用刚刚创建的方法,并应用角半径(在本例中它将是一个圆):

imageView.image = [self squareImageWithImage:imageNormal scaledToSize:CGSizeMake(50, 50)]; // assuming you want a 50x50px image
// convert imageview to circle
imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2;
imageView.clipsToBounds = YES;

您可以使用相同的调用将 cornerRadius 的数量减少到其他值。

关于ios - 如何使 imageView 固定大小并带有圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27234573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com