我试图在UIImageView上得到一个不错的圆角。我找到了以下代码的实现,如果图像是正方形的,则可以正常工作。问题是我的imageView是一张矩形照片,有时是肖像,有时是风景。这段代码可以消除角落,但是当图像的一侧比另一侧长时,则不会给出平滑的曲线。有任何想法吗? setCornerRadius中的float是否也以边的百分比或仅以直线像素计数的形式应用于图像?

    // Get the Layer of any view
    CALayer * l = [imageView layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:100.0];

这是所有有兴趣的人的解决方法。

好吧,经过一番尝试,我发现我需要调整imageView的大小以适合图像。我实现了以下方法来设置帧大小。
- (CGRect)getScaleForFrameFromImage:(UIImage *)image
{
    // get the bigger side of image to determine the shape then
    // get the percentage we need to scale to to trim imageViewFrame
    // so it fits image and sits in the space dedicated in main view for the image
    float percentage;
    float newWidth;
    float newHeight;

    float w = image.size.width;
    float h = image.size.height;
    if (w > h) {
        // landscape
        percentage  = 280 / w;
        newWidth = w * percentage;
        newHeight = h * percentage;;
    }
    else {
        percentage  = 208 / h;
        newWidth = w * percentage;
        newHeight = h * percentage;
    }
    int xOrigin = 20 + (280 - newWidth) / 2;
    CGRect newFrame = CGRectMake(xOrigin, 160, newWidth, newHeight);
    return newFrame;
}

然后在我看来WillAppear我做到了
    // set the imageFrame size
    [imageView setFrame:[self getScaleForFrameFromImage:imageToDisplay]];

    // Use that image to put on the screen in imageView
    [imageView setImage:imageToDisplay];

    // Get the Layer of imageView
    CALayer * l = [imageView layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:10.0];

最佳答案

我建议用图像的大小绘制imageView,然后应用拐角半径。

就像这样。

// get the size of the image.
CGSize *size = yourImage.size;

// use the size to set the uiimageview frame
UIImageView * imageView = [UIImageView alloc] initWithFrame:CGRecMake(0,0,size.widht,size.height)];
 [imageView setImage:yourImage];
//
CALayer * l = [imageView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:100.0];

关于objective-c - 如何在非正方形的Objective C UIImageView上获得平滑的拐角半径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9824450/

10-13 04:23