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

Ios 如何在椭圆形或圆形上裁剪UIImage?,ios,objective-c,swift,iphone,ios4,Ios,Objective C,Swift,Iphone,Ios4,请给出如何在椭圆形或圆形上裁剪UIImage。请分享您的想法。查看CGImageCreateWithMask。创建椭圆形状的遮罩,然后将其应用于图像。您应该参考 您还需要添加 [imageView.layer setMasksToBounds:YES]; Swift 4.2 import QuartzCore var imageLayer: CALayer? = YourImageview.layer imageLayer?.cornerRadius = 5 imageLayer?.bord

请给出如何在椭圆形或圆形上裁剪
UIImage
。请分享您的想法。

查看
CGImageCreateWithMask
。创建椭圆形状的遮罩,然后将其应用于图像。

您应该参考

您还需要添加

[imageView.layer setMasksToBounds:YES];
Swift 4.2

import QuartzCore

var imageLayer: CALayer? = YourImageview.layer
imageLayer?.cornerRadius = 5
imageLayer?.borderWidth = 1
imageLayer?.masksToBounds = true

经过长时间的搜索,我找到了圈出图像的正确方法

从URL下载支持归档文件

以下行用于调整图像大小并转换为带半径的圆形

UIImage *mask = [UIImage imageNamed:@"mask.jpg"];

mask = [mask resizedImage:CGSizeMake(47, 47) interpolationQuality:kCGInterpolationHigh ];
mask = [mask roundedCornerImage:23.5 borderSize:1];

几周前我开始调查这件事。我在这里尝试了所有的建议,没有一个效果很好。在RTFM的伟大传统中,我阅读了苹果公司的文档,并提出了这个。请试一试,让我知道你的进展如何

代码可以很容易地修改为裁剪为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;
}

如果您只需要一个完美的圆,更改UIImageView的形状可能会有所帮助。 只需将QuartzCore框架添加到项目中,并在显示imageView之前在生命周期的某个位置添加以下代码行:

#import <QuartzCore/QuartzCore.h>
.
.
.

//to crop your UIImageView to show only a circle
yourImageView.layer.cornerRadius = yourImageView.frame.size.width/2;
yourImageView.clipsToBounds = YES;
#导入
.
.
.
//将UIImageView裁剪为仅显示圆的步骤
yourImageView.layer.cornerRadius=yourImageView.frame.size.width/2;
yourImageView.clipsToBounds=是;

制作圆形图像

@property (strong, nonatomic) IBOutlet UIImageView *songImage;
- (void)viewDidLoad
{
    self.songImage.layer.cornerRadius = self.songImage.frame.size.width / 2;
    self.songImage.clipsToBounds = YES;
    
     //To give the Border and Border color of imageview

   self.songImage.layer.borderWidth = 1.0f;
   self.songImage.layer.borderColor = [UIColor colorWithRed:249/255.0f green:117/255.0f blue:44/255.0f alpha:1.0f].CGColor;

}
步骤1:in.h文件

@property (strong, nonatomic) IBOutlet UIImageView *songImage;
- (void)viewDidLoad
{
    self.songImage.layer.cornerRadius = self.songImage.frame.size.width / 2;
    self.songImage.clipsToBounds = YES;
    
     //To give the Border and Border color of imageview

   self.songImage.layer.borderWidth = 1.0f;
   self.songImage.layer.borderColor = [UIColor colorWithRed:249/255.0f green:117/255.0f blue:44/255.0f alpha:1.0f].CGColor;

}
步骤2:in.m文件

@property (strong, nonatomic) IBOutlet UIImageView *songImage;
- (void)viewDidLoad
{
    self.songImage.layer.cornerRadius = self.songImage.frame.size.width / 2;
    self.songImage.clipsToBounds = YES;
    
     //To give the Border and Border color of imageview

   self.songImage.layer.borderWidth = 1.0f;
   self.songImage.layer.borderColor = [UIColor colorWithRed:249/255.0f green:117/255.0f blue:44/255.0f alpha:1.0f].CGColor;

}
或用于Swift

cell.songImage.layer.cornerRadius=cell.songImage.frame.size.width/2;
cell.songImage.clipsToBounds=true
//给出imageview的边框和边框颜色的步骤
cell.songImage.layer.borderWidth=1.0
cell.songImage.layer.borderColor=UIColor(红色:50.0/255,绿色:150.0/255,蓝色:65.0/255,alpha:1.0)

将imageView设置为椭圆形并不困难

您可以执行以下操作

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:yourImageView.bounds];
CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.path = path.CGPath;
yourImageView.layer.mask = maskLayer;
如果传递给bezierPathWithOvalInRect的矩形为方形,则图像将被裁剪为圆形

Swift代码

let path = UIBezierPath(ovalIn: yourImageView.bounds)
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
yourImageView.layer.mask = maskLayer
SWIFT

var vwImage = UIImageView(image: UIImage(named: "Btn_PinIt_Normal.png"))
vwImage.frame = CGRectMake(0, 0, 100, 100)
vwImage.layer.cornerRadius = vwImage.frame.size.width/2

SWIFT 3答案来自@Mohammad Sadiq

let path = UIBezierPath.init(ovalIn: workingImgaeView.bounds)
let maskLayer = CAShapeLayer(layer: layer)
maskLayer.path = path.cgPath
workingImgaeView.layer.mask = maskLayer
这应该行得通, 尝试在viewDidLoad()中粘贴以下代码


如何在我的图像上分开椭圆形?请帮助我是的,你必须把“rect”做成椭圆形。。。请阅读我的评论(“此处为椭圆形逻辑”)(“此处为椭圆形逻辑”)-此链接不起作用。请帮助我如何实施,请帮助我,我不知道。。我是iphone sdk新手,请帮助我。您缺少
[imageLayer setMasksToBounds:是]:)是的,您必须使用Andres的建议将图像剪裁成imageView的形状只要图像是正方形,您可以将一半宽度作为角半径得到一个完美的圆:[imageView.layer setCornerRadius:imageView.frame.size.width/2];self.imageView.layer.masksToBounds=是;我想裁剪成圆形的图像。因此,我们只能看到圆形路径,而其他路径保持透明。此动态答案比所选答案要好得多。我可以使用uiview而不是uiimage将其设置为椭圆形吗?如何裁剪目标c的椭圆形图像?这应该是可接受的答案,因为它实际上解决了椭圆形的问题(并且简洁)
let path = UIBezierPath.init(ovalIn: workingImgaeView.bounds)
let maskLayer = CAShapeLayer(layer: layer)
maskLayer.path = path.cgPath
workingImgaeView.layer.mask = maskLayer
self.myImage.layer.cornerRadius = self.myImage.frame.size.width / 2;
self.myImage.clipsToBounds = YES;