【问题标题】:How to crop UIImage on oval shape or circle shape?如何在椭圆形或圆形上裁剪 UIImage?
【发布时间】:2011-09-25 17:05:30
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c swift iphone ios4


    【解决方案1】:

    制作圆形图像

    第一步:在.h文件中

    @property (strong, nonatomic) IBOutlet UIImageView *songImage;
    

    第二步:在.m文件中

    - (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
                            
    //To give the Border and Border color of imageview
    cell.songImage.layer.borderWidth = 1.0
    cell.songImage.layer.borderColor = UIColor(red: 50.0/255, green: 150.0/255, blue: 65.0/255, alpha: 1.0).CGColor
    

    【讨论】:

      【解决方案2】:

      这应该有效, 尝试在 viewDidLoad() 中粘贴以下代码。

      self.myImage.layer.cornerRadius = self.myImage.frame.size.width / 2;
      self.myImage.clipsToBounds = YES;
      

      【讨论】:

        【解决方案3】:
        #import <QuartzCore/QuartzCore.h>
        
        CALayer *imageLayer = YourImageview.layer;
                [imageLayer setCornerRadius:5];
                [imageLayer setBorderWidth:1];
                [imageLayer setMasksToBounds:YES];
        

        通过增加半径,它会变得更圆。

        只要图片是正方形,取一半宽度作为圆角半径就可以得到一个正圆:

        [imageView.layer setCornerRadius:imageView.frame.size.width/2]; 
        

        你还需要添加

        [imageView.layer setMasksToBounds:YES];
        

        斯威夫特 4.2

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

        【讨论】:

        • 你不见了[imageLayer setMasksToBounds:YES]; :)
        • 是的,您必须使用 Andres 的建议将图像剪辑成 ImageView 的形状
        • 只要图片是正方形,取一半宽度作为圆角半径就可以得到一个正圆:[imageView.layer setCornerRadius:imageView.frame.size.width/2]; self.imageView.layer.masksToBounds = YES;
        • 我想将图像裁剪成圆形。这样我们就只能看到圆形路径,其他路径保持透明。
        【解决方案4】:

        拥有椭圆形的imageView并不难。

        您可以执行以下操作

        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:yourImageView.bounds];
        CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
        maskLayer.path = path.CGPath;
        yourImageView.layer.mask = maskLayer;
        

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

        Swift 代码

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

        【讨论】:

        • 我可以用 uiview 代替 uiimage 使它变成椭圆形吗?
        • 如何裁剪目标c的椭圆形图像?
        • 这应该是公认的答案,因为它实际上解决了椭圆形的情况(并且简洁)
        【解决方案5】:

        SWIFT 3 答案来自@Mohammad Sadiq

        let path = UIBezierPath.init(ovalIn: workingImgaeView.bounds)
        let maskLayer = CAShapeLayer(layer: layer)
        maskLayer.path = path.cgPath
        workingImgaeView.layer.mask = maskLayer
        

        【讨论】:

          【解决方案6】:

          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
          

          【讨论】:

            【解决方案7】:

            如果您只需要一个完美的圆形,更改 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;
            

            【讨论】:

            • 这个动态答案比选择的答案要好得多。
            【解决方案8】:

            几周前我开始研究这个问题。我在这里尝试了所有建议,但没有一个效果很好。在 RTFM 的伟大传统中,我去阅读了 Apple 在Quartz 2D Programming 上的文档并想出了这个。请尝试一下,然后告诉我你的情况。

            代码可以很容易地修改为椭圆或路径定义的任何其他形状。

            确保在项目中包含 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;
            }
            

            在您的 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];
            
            }
            

            【讨论】:

              【解决方案9】:

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

              从 URL http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ 下载支持存档文件

              #import "UIImage+RoundedCorner.h"
              #import "UIImage+Resize.h"
              

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

              UIImage *mask = [UIImage imageNamed:@"mask.jpg"];
              
              mask = [mask resizedImage:CGSizeMake(47, 47) interpolationQuality:kCGInterpolationHigh ];
              mask = [mask roundedCornerImage:23.5 borderSize:1];
              

              【讨论】:

                【解决方案10】:

                你应该参考This ...

                // Create the image from a png file
                UIImage *image = [UIImage imageNamed:@"prgBinary.jpg"];
                UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
                
                // Get size of current image
                CGSize size = [image size];
                
                // Frame location in view to show original image
                [imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
                [[self view] addSubview:imageView];
                [imageView release];    
                
                // Create rectangle that represents a cropped image  
                // from the middle of the existing image
                CGRect rect = CGRectMake(size.width / 4, size.height / 4 , 
                (size.width / 2), (size.height / 2)); //oval logic goes here
                
                // Create bitmap image from original image data,
                // using rectangle to specify desired crop area
                CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
                UIImage *img = [UIImage imageWithCGImage:imageRef]; 
                CGImageRelease(imageRef);
                
                // Create and show the new image from bitmap data
                imageView = [[UIImageView alloc] initWithImage:img];
                [imageView setFrame:CGRectMake(0, 200, (size.width / 2), (size.height / 2))];
                [[self view] addSubview:imageView];
                [imageView release];
                

                【讨论】:

                • 是的,你必须把“rect”做成椭圆形...阅读我的 cmets(“这里是椭圆形逻辑”)
                • ("此处为椭圆逻辑") -此链接无效。请帮我maulik
                • 如何实现,请帮助我,我不知道..我是 iphone sdk 的新手,请帮助我。
                【解决方案11】:

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

                【讨论】:

                • 如何在我的图像上分离椭圆形?请帮助我
                猜你喜欢
                • 2011-09-19
                • 2012-04-21
                • 2014-05-01
                • 2017-07-05
                • 2019-08-08
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-04-27
                相关资源
                最近更新 更多