iPhone以编程方式裁剪方形图像以显示为圆形

Ale*_*one 12 iphone crop objective-c uiimage ios

我正在尝试使用iPhone上的相机胶卷中的图像为自定义样式UIButton创建图像.该按钮具有圆形背景并有效地显示为圆形.现在我需要一个图像进入按钮的中间,该图像也显示为圆形.

如何在圆形区域外切割方形UIImage以显示圆形?

如果涉及到屏蔽,我是否需要预先渲染一个掩码,或者我可以以编程方式创建一个(例如:一个圆圈)?

谢谢!

Nov*_*arg 28

我从未做过类似的事情,但尝试使用QuartzCore框架及其cornerRadius属性.例:

#import <QuartzCore/QuartzCore.h>
//some other code ...
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
imgView.layer.cornerRadius = 10.0f;
Run Code Online (Sandbox Code Playgroud)

稍微玩一下,你会得到你想要的.

希望能帮助到你

  • @AlexStone - 你需要imgView.clipsToBounds = YES (12认同)
  • 另外,对于一个圆圈,cornerRadius = imageView.frame.size.width/2 (12认同)

cal*_*kus 13

是的,您可以使用CoreGraphics动态绘制蒙版.然后,您可以创建蒙版图像.

掩蔽示例:

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage
{
  CGImageRef maskRef = maskImage.CGImage;
  CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                      CGImageGetHeight(maskRef),
                                      CGImageGetBitsPerComponent(maskRef),
                                      CGImageGetBitsPerPixel(maskRef),
                                      CGImageGetBytesPerRow(maskRef),
                                      CGImageGetDataProvider(maskRef), NULL, false);

  CGImageRef maskedImageRef = CGImageCreateWithMask([image CGImage], mask);
  UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef];
  CGImageRelease(maskedImageRef);
  CGImageRelease(mask);
  return maskedImage;
}
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记释放创建的蒙版和图像,否则你将泄漏内存(*ARC*不处理*CoreGraphics*的不透明类型).这样做`CGImageRef maskedImageRef = CGImageCreateWithMask([image CGImage],mask); UIImage*maskedImage = [UIImage imageWithCGImage:masked]; CGImageRelease(maskImageRef); CGImageRelease(maskedImageRef); return maskedImage;`. (2认同)

小智 7

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

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

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

#include <math.h>

+ (UIImage*)circularScaleNCrop:(UIImage*)image: (CGRect) rect{
    // 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(rect.size.width, rect.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 = rect.size.width;
    CGFloat rectHeight = rect.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 circularScaleNCrop:originalImage :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)


小智 6

这是在方形ImageView上创建圆角的快速方法,使其看起来像一个完美的圆形.基本上你应用的角半径等于宽度的1/2(方形图像上的宽度==高度).

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

float width = imageView.bounds.size.width;  // we can also use the frame property instead of bounds since we just care about the Size and don't care about position
imageView.layer.cornerRadius = width/2;
Run Code Online (Sandbox Code Playgroud)


AP_*_*AP_ 6

{
    imageView.layer.cornerRadius =  imageView.frame.size.height /2;
    imageView.layer.masksToBounds = YES;
    imageView.layer.borderWidth = 0;
}
Run Code Online (Sandbox Code Playgroud)


Nik*_*kov 5

UIImage类别用圆圈掩盖图像:

UIImage *originalImage = [UIImage imageNamed:@"myimage.png"];
UImage *myRoundedImage = [UIImage roundedImageWithImage:originalImage];
Run Code Online (Sandbox Code Playgroud)

得到它在这里.