如何在椭圆形或圆形上裁剪UIImage?

ram*_*ram 39 iphone objective-c ios4 ios swift

请提供有关如何裁剪UIImage椭圆形或圆形的想法.请分享您的想法.

Rak*_*att 109

#import <QuartzCore/QuartzCore.h>

CALayer *imageLayer = YourImageview.layer;
        [imageLayer setCornerRadius:5];
        [imageLayer setBorderWidth:1];
        [imageLayer setMasksToBounds:YES];
Run Code Online (Sandbox Code Playgroud)

通过增加半径,它将变得更圆.

只要图像是正方形,您可以通过将宽度的一半作为角半径来获得完美的圆:

[imageView.layer setCornerRadius:imageView.frame.size.width/2]; 
Run Code Online (Sandbox Code Playgroud)

你还需要添加

[imageView.layer setMasksToBounds:YES];
Run Code Online (Sandbox Code Playgroud)

Swift 4.2

import QuartzCore

var imageLayer: CALayer? = YourImageview.layer
imageLayer?.cornerRadius = 5
imageLayer?.borderWidth = 1
imageLayer?.masksToBounds = true
Run Code Online (Sandbox Code Playgroud)

  • 只要图像是正方形,您就可以通过将宽度的一半作为角半径来获得完美的圆:[imageView.layer setCornerRadius:imageView.frame.size.width/2]; self.imageView.layer.masksToBounds = YES; (18认同)
  • 你错过了`[imageLayer setMasksToBounds:YES];`:) (6认同)

小智 36

几个星期前我开始研究这个问题.我在这里尝试了所有的建议,其中没有一个效果很好.在RTFM的伟大传统中,我去阅读Apple关于Quartz 2D Programming的文档并提出了这个问题.请试一试,让我知道你怎么走.

代码可以很容易地修改为裁剪为elipse,或由路径定义的任何其他形状.

确保在项目中包含Quartz 2D.

#include <math.h>

+ (UIImage*)circularScaleAndCropImage:(UIImage*)image frame:(CGRect)frame {
    // This function returns a newImage, based on image, that has been:
    // - scaled to fit in (CGRect) rect
    // - and cropped within a circle of radius: rectWidth/2

    //Create the bitmap graphics context
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(frame.size.width, frame.size.height), NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    //Get the width and heights
    CGFloat imageWidth = image.size.width;
    CGFloat imageHeight = image.size.height;
    CGFloat rectWidth = frame.size.width;
    CGFloat rectHeight = frame.size.height;

    //Calculate the scale factor
    CGFloat scaleFactorX = rectWidth/imageWidth;
    CGFloat scaleFactorY = rectHeight/imageHeight;

    //Calculate the centre of the circle
    CGFloat imageCentreX = rectWidth/2;
    CGFloat imageCentreY = rectHeight/2;

    // Create and CLIP to a CIRCULAR Path
    // (This could be replaced with any closed path if you want a different shaped clip)
    CGFloat radius = rectWidth/2;
    CGContextBeginPath (context);
    CGContextAddArc (context, imageCentreX, imageCentreY, radius, 0, 2*M_PI, 0);
    CGContextClosePath (context);
    CGContextClip (context);

    //Set the SCALE factor for the graphics context
    //All future draw calls will be scaled by this factor
    CGContextScaleCTM (context, scaleFactorX, scaleFactorY);    

    // Draw the IMAGE
    CGRect myRect = CGRectMake(0, 0, imageWidth, imageHeight);
    [image drawInRect:myRect];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}
Run Code Online (Sandbox Code Playgroud)

在您的UIView类中包含以下代码,将"monk2.png"替换为您自己的图像名称.

- (void)drawRect:(CGRect)rect {

    UIImage *originalImage = [UIImage imageNamed:[NSString stringWithFormat:@"monk2.png"]];
    CGFloat oImageWidth = originalImage.size.width;
    CGFloat oImageHeight = originalImage.size.height;
    // Draw the original image at the origin
    CGRect oRect = CGRectMake(0, 0, oImageWidth, oImageHeight);
    [originalImage drawInRect:oRect];

    // Set the newRect to half the size of the original image 
    CGRect newRect = CGRectMake(0, 0, oImageWidth/2, oImageHeight/2);
    UIImage *newImage = [self circularScaleAndCropImage:originalImage frame:newRect];

    CGFloat nImageWidth = newImage.size.width;
    CGFloat nImageHeight = newImage.size.height;

    //Draw the scaled and cropped image
    CGRect thisRect = CGRectMake(oImageWidth+10, 0, nImageWidth, nImageHeight);
    [newImage drawInRect:thisRect];

}
Run Code Online (Sandbox Code Playgroud)


Moh*_*diq 11

使imageView呈椭圆形并不困难.

您可以执行以下操作

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:yourImageView.bounds];
CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.path = path.CGPath;
yourImageView.layer.mask = maskLayer;
Run Code Online (Sandbox Code Playgroud)

如果传递给bezierPathWithOvalInRect的rect是Square,则图像将被裁剪为圆形.

SWIFT代码

let path = UIBezierPath(ovalIn: yourImageView.bounds)
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
yourImageView.layer.mask = maskLayer
Run Code Online (Sandbox Code Playgroud)