Draw a simple circle uiimage

Thanks for the Q&A! Swift code as below: extension UIImage { class func circle(diameter: CGFloat, color: UIColor) -> UIImage { UIGraphicsBeginImageContextWithOptions(CGSizeMake(diameter, diameter), false, 0) let ctx = UIGraphicsGetCurrentContext() CGContextSaveGState(ctx) let rect = CGRectMake(0, 0, diameter, diameter) CGContextSetFillColorWithColor(ctx, color.CGColor) CGContextFillEllipseInRect(ctx, rect) CGContextRestoreGState(ctx) let img = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return img } } Swift 3 version provided by … Read more

Cut transparent hole in UIView

This is my implementation (as I did needed a view with transparent parts): Header (.h) file: // Subclasses UIview to draw transparent rects inside the view #import <UIKit/UIKit.h> @interface PartialTransparentView : UIView { NSArray *rectsArray; UIColor *backgroundColor; } – (id)initWithFrame:(CGRect)frame backgroundColor:(UIColor*)color andTransparentRects:(NSArray*)rects; @end Implementation (.m) file: #import “PartialTransparentView.h” #import <QuartzCore/QuartzCore.h> @implementation PartialTransparentView – (id)initWithFrame:(CGRect)frame backgroundColor:(UIColor*)color … Read more

Setting A CGContext Transparent Background

After UIGraphicsGetCurrentContext() call CGContextClearRect(context,rect) Edit: Alright, got it. Your custom view with the line should have the following: – (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setBackgroundColor:[UIColor clearColor]]; } return self; } – (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextClearRect(context, rect); CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); CGContextSetLineWidth(context, 5.0); CGContextMoveToPoint(context, 100.0,0.0); CGContextAddLineToPoint(context,100.0, 100.0); CGContextStrokePath(context); } My … Read more

Changing UIImage color

Since iOS 7, this is the most simple way of doing it. Objective-C: theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [theImageView setTintColor:[UIColor redColor]]; Swift 2.0: theImageView.image = theImageView.image?.imageWithRenderingMode(.AlwaysTemplate) theImageView.tintColor = UIColor.magentaColor() Swift 4.0: theImageView.image = theImageView.image?.withRenderingMode(.alwaysTemplate) theImageView.tintColor = .magenta Storyboard: First configure the image as template ( on right bar – Render as) in your assets. Then the … Read more