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:
Create a UIBezierPath using bezierPathWithOvalInRect
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:YOUR_RECT];
Create a mask layer by using CAShapeLayer
CAShapeLayer *maskLayer = [CAShapeLayer layer];
Now set the bezier path as mask layer's path
maskLayer.path = path.CGPath;
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
**.
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
Stop Uiwebview from "Bouncing" Vertically
How to Disable Back Swipe Gesture in Uinavigationcontroller on iOS 7
How to Implement a Pop-Up Dialog Box in iOS
Core Data Background Context Best Practice
How to Create a Uiview Bounce Animation
Cocoapods - 'Pod Install' Takes Forever
Uitableview: How to Disable Selection for Some Rows But Not Others
How to Draw a Transparent Uitoolbar or Uinavigationbar in iOS7
No Avplayer Delegate? How to Track When Song Finished Playing? Objective C iPhone Development
How to Create Layout Constraints Programmatically
iOS 8 Rotation Methods Deprecation - Backwards Compatibility
How to Create Managedobjectcontext Using Swift 3 in Xcode 8
One Step Affine Transform for Rotation Around a Point
Uicollectionview: Must Be Initialized with a Non-Nil Layout Parameter
iOS App Freezes on Pushviewcontroller
How to Change Color of Single Word in Uitextview and Uitextfield