How to Crop Uiimage on Oval Shape or Circle Shape

How to crop UIImage on oval shape or circle shape?

#import 

CALayer *imageLayer = YourImageview.layer;
[imageLayer setCornerRadius:5];
[imageLayer setBorderWidth:1];
[imageLayer setMasksToBounds:YES];

by increasing radius it will become more round-able.

As long as the image is a square, you can get a perfect circle by taking half the width as the corner radius:

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

You also need to add

[imageView.layer setMasksToBounds:YES];

Swift 4.2

import QuartzCore

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

how to draw oval shape in UIImageview

If you want to draw an imageview in an oval shape, follow below steps:

  1. Create a UIBezierPath using bezierPathWithOvalInRect

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:YOUR_RECT];
  2. Create a mask layer by using CAShapeLayer

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
  3. Now set the bezier path as mask layer's path

    maskLayer.path = path.CGPath;
  4. Then we are going mask our view with our own mask layer.

    YourImageview.layer.mask = maskLayer;

That's all. Try it.

Crop circular or elliptical image from original UIImage

There are a pile of blend modes to choose from, a few of which are useful for "masking". I believe this should do approximately what you want:

CGContextSaveGState(contextRef);
CGContextSetBlendMode(contextRef,kCGBlendModeDestinationIn);
CGContextFillEllipseInRect(contextRef,face_rect);
CGContextRestoreGState(contextRef);

"approximately" because it'll mask the entire context contents every time, thus doing the wrong thing for more than one face. To handle this case, use CGContextAddEllipseInRect() in the loop and CGContextFillPath() at the end.

You might also want to look at CGContextBeginTransparencyLayerWithRect().

Crop image in oval shape

- (UIImage *)croppedPhoto {
// For dealing with Retina displays as well as non-Retina, we need to check
// the scale factor, if it is available. Note that we use the size of teh cropping Rect
// passed in, and not the size of the view we are taking a screenshot of.
CGRect croppingRect = CGRectMake(imgMaskImage.frame.origin.x,
imgMaskImage.frame.origin.y, imgMaskImage.frame.size.width,
imgMaskImage.frame.size.height);

imgMaskImage.hidden=YES;

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(croppingRect.size, YES, [UIScreen mainScreen].scale);
} else {
UIGraphicsBeginImageContext(croppingRect.size);
}

// Create a graphics context and translate it the view we want to crop so
// that even in grabbing (0,0), that origin point now represents the actual
// cropping origin desired:
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -croppingRect.origin.x, -croppingRect.origin.y);
[self.view.layer renderInContext:ctx];

// Retrieve a UIImage from the current image context:
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// Return the image in a UIImageView:
return snapshotImage;
}

iPhone programmatically crop a square image to appear as circle

Yes you can use CoreGraphics to draw the mask dynamically.
Then you can create the masked image.

Example for masking:

- (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;
}

Cut a UIImage into a circle

Make sure to import QuarzCore if needed.

 func maskRoundedImage(image: UIImage, radius: CGFloat) -> UIImage {
let imageView: UIImageView = UIImageView(image: image)
let layer = imageView.layer
layer.masksToBounds = true
layer.cornerRadius = radius
UIGraphicsBeginImageContext(imageView.bounds.size)
layer.render(in: UIGraphicsGetCurrentContext()!)
let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return roundedImage!
}

Crop image to be circular swift 3

Lets get on **Graphics Context
**.

Sample Image

func makeRoundImg(img: UIImageView) -> UIImage {
let imgLayer = CALayer()
imgLayer.frame = img.bounds
imgLayer.contents = img.image?.cgImage;
imgLayer.masksToBounds = true;

imgLayer.cornerRadius = 28 //img.frame.size.width/2

UIGraphicsBeginImageContext(img.bounds.size)
imgLayer.render(in: UIGraphicsGetCurrentContext()!)
let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return roundedImage!;
}


Related Topics



Leave a reply



Submit