FAQ Database Discussion Community


How do I update a property on UIView, using a pop-out view controller?

ios,swift,uiview,core-graphics
I'm trying to add a stroke width pop out menu to a simple drawing app using core graphics in Swift. How do I set the StrokeWidthViewController to update the strokeWidth property on DrawView.swift (UIView)? I know how to pass data between view controllers, but I've never passed data to a...

Animation Direction on iOS [closed]

ios,core-graphics,core-animation
I know everything about animation Tools in iOS. Rotation, Scaling and Moving but I have made a bad timing for example I have two Squares one on the Left and the Other on the Right and i want the two squares two rotates until they hit each other with Angle...

Apply gradient across multiple UITableViewCells

ios,uitableview,core-graphics,linear-gradients
I have a UITableView with a white background, I would like to apply a linear gradient across the tableview cells so that the gradient is one single gradient from the start to end of the tableview see an example below from Adobe Illustrator. Is there a way this can be...

Large UIBezierPath, slow rendering

ios,cocoa-touch,graphics,core-graphics,uibezierpath
For an app I am creating, I am using a UIBezierPath to store a path. The problem is that this path continually increases in length, and at a point, the app lags and becomes jerky. Because the path is continually increasing in length, I am constantly redrawing the view 100...

Blend mode kCGBlendModeMultiply is not correctly multiplying an image with a color

ios,osx,swift,core-graphics,swift-playground
Goal: To tint an image by multiplying it with a color. Assumptions: According to Apple's documentation, the blend mode kCGBlendModeMultiply "multiplies the source image samples with the background image samples." What I understand from this is: R = S * D for R, G, B and A. So if one...

CGRectMake Symbol not Found

osx,core-graphics,core-foundation,jsctypes
I'm trying to declare some CoreGraphics functions, specificially CGRectMake and it keeps telling me symbol not found. The docs say I need to import CoreGraphics: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGGeometry/index.html#//apple_ref/c/func/CGRectMake The path to my CoreGraphics is /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation can you please help me find the right library for this function? I am doing this from...

Create Hexagon ImageView shape in iOS

ios,uiimageview,uiimage,core-graphics,uibezierpath
I want Above Hexagon shape for my ImageView. But After implementing below code I am getting this Image - (UIBezierPath *)roundedPolygonPathWithRect:(CGRect)square lineWidth:(CGFloat)lineWidth sides:(NSInteger)sides cornerRadius:(CGFloat)cornerRadius { UIBezierPath *path = [UIBezierPath bezierPath]; CGFloat theta = 2.0 * M_PI / sides; // how much to turn at every corner CGFloat offset =...

Why doesn't CGContextSetStrokeColorWithColor set text colour to black?

ios,objective-c,core-graphics
I have drawn text labels in front of a circular arc of dots using iOS Core Graphics. I am trying to understand why CGContextSetStrokeColorWithColor does not set text colour to black in the two examples below. Text labels are black if the dots are outlined. But when images are filled...

Swift - Core graphics and setting background color?

ios,swift,core-graphics
I created this method that generates and image out of an original one but adds padding to the new image according to a given size. Everything works fine except the background color of the image is always black although I set white as fill color. Any idea how to fix...

Karaoke-like progress highlight for iOS

ios,cocoa-touch,core-graphics,core-animation,core-text
I would like to implement karaoke-like progress highlight for iOS. I know I could use NSAttributedString and highlight the text character by character. However, I would like the highlight to progress pixel by pixel, not character by character. Any ideas? P.S. No need for sample code, just point me to...

CGDisplayCopyAllDisplayModes leaves out one valid mode

osx,swift,core-graphics
When programmatically working with display modes in OS X (documentation), I've found that the CGDisplayCopyAllDisplayModes leaves out the rightmost option that is presented in System Preferences. A simple utility that prints the size of the current display mode and all available display mode sizes outputs this current size: 1920x1200 available...

How to draw an arrow inside a disk?

ios,swift,core-graphics,uibezierpath
I would like to draw an arrow inside a plain disk. Or more precisely, I would like to remove the shape of an arrow from a disk (we could see through the disk where the arrow is drawn), like this one: How can I achieve this? UPDATE: So far I...

Incorrectly using Core Graphics

swift,ios8,core-graphics
I am trying to make a custom UISlider by creating a class that extends UIControl. I am having issues trying to fill and stroke CGRects using core graphics. I am able to correctly stroke and fill a CGRect at position (0,0), but cannot at any other position. Here is what...

How to save a CoreGraphics Drawing in Swift

ios,swift,uiview,save,core-graphics
I want to make an application where the user can draw on the screen and then save their drawings. Below is my UIView draw code: import UIKit class DrawView: UIView { var lines: [Line] = [] // Line is a custom class, shown below var lastPoint: CGPoint! required init(coder aDecoder:...

Add a semi-transparent layer to UIImage using Core Graphics

ios,objective-c,uiimage,core-graphics,alpha
I need to take a UIImage and to add a semi-transparent layer in order to produce a new UIImage. I think I'm getting close but something is still wrong. Here's my code: - (UIImage*) addLayerTo:(UIImage*)source { CGSize size = [source size]; UIGraphicsBeginImageContext(size); CGRect rect = CGRectMake(0, 0, size.width, size.height); [source...

iOS app working without essential frameworks linked

ios,xcode,frameworks,core-graphics,core-image
i am working on an iOS app which is able to make videos out of single screenshots. I therefore need things like CVPixelBufferRef, CGContextRef and AVAssetWriter. Currently the app is working fine but there is one thing I do not understand: For example, CVPixelBufferRef is a reference to a Core...

iOS record audio and draw waveform like Voice Memos

ios,opengl-es,core-graphics,core-audio,avaudiorecorder
I'm going to ask this at the risk of being too vague or asking too many things in one question, but I'm really just looking for a point in the right direction. In my app I want to record audio, show a waveform while recording, and scroll through the waveform...

How to cut the shape of a logo(png image) from another image programmatically in Swift?

ios,swift,uiimage,core-graphics
This is the background image This is the logo How can we make a image like this in Swift? Update Now I manage to use the logo as a mask and get something like this, Is there any way to reverse the mask? Here is my code let logo =...

Why is CG Image not being released?

objective-c,xcode,osx,core-graphics
I've been banging my head for some time on a larger project with a memory leak. I've managed to create a very basic CLI app that duplicates the issue. Why is CG Image not being released? The real app would actually create a bunch of thumbs of various sizes. This...

Pixelated layer on image in ios

ios,objective-c,iphone,core-graphics
I need to add pixelated rectangular layer on UIImage which can be undo. Just like this.. I used this code but its not doing the same thing as i need CALayer *maskLayer = [CALayer layer]; CALayer *mosaicLayer = [CALayer layer]; // Mask image ends with 0.15 opacity on both sides....

UIBezierPath vs putting png in imageassets

ios,core-graphics,uibezierpath,paint-code
I'm helping create an app that will use images that can be resized (think AutoCAD). Fortunately, I have PaintCode and I have Illustrator so it's very easy for me to convert a svg file into code should I want to. I converted one image into code and it's around 10,000...

Using Core Graphics from ObjectiveC, how to you curve an arrow around a circle?

ios,objective-c,core-graphics,drawrect,curve
Our designer has asked me to recreate this: Im subclassing UIView, and I've overridden the drawRect command like this: [super drawRect:frame]; CGFloat x = self.frame.origin.x; CGFloat y = self.frame.origin.y; CGFloat w = self.frame.size.width; CGFloat h = self.frame.size.height; CGFloat lineWidth = lineWidthRequested; CGPoint centerPoint = CGPointMake(w/2, h/2); CGFloat radius = radiusRequested;...

CGContextClearRect causing background color to be black

ios,objective-c,uiview,uibutton,core-graphics
I have a UIButton subclass that I am drawing borders on using drawRect:. When the button is selected I want the background color to change, and the borders removed. (Drawing borders manually because sometimes I only need one border on one side) For some reason when I use CGContextClearRect to...

How to add custom color to a shape made with core graphics?

swift,core-graphics
I have made a shape with core graphics and I want to fill it with a custom color. For example instead of default red i want dark red. This is the code for the shape and color: override func drawRect(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextSetLineWidth(context, 3.0) CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor)...

How to create grayscale CGImage from iPhone Camera?

ios,swift,core-graphics,avfoundation
I want to create grayscale CGImage from iPhones camera data. I'm using AVFoundation and set pixel format type to kCVPixelFormatType_420YpCbCr8BiPlanarFullRange If I understand correctly I should be able to create grayscale image from Y component so I have implemented delegate method like this: func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection...

How do you get Unmanaged from CGColorSpace?

ios,swift,core-graphics,vimage
I am writing a function in Swift that creates a vImage_CGImageFormat from a CGImage as follows: vImage_CGImageFormat( bitsPerComponent: UInt32(CGImageGetBitsPerComponent(image)), bitsPerPixel: UInt32(CGImageGetBitsPerPixel(image)), colorSpace: CGImageGetColorSpace(image), bitmapInfo: CGImageGetBitmapInfo(image), version: UInt32(0), decode: CGImageGetDecode(image), renderingIntent: CGImageGetRenderingIntent(image)) This doesn't compile however. That's because CGImageGetColorSpace(image) returns CGColorSpace! and the above constructor only...

How can I animate some subviews of a UIViewController “off” the screen, and other views “on” the screen?

ios,objective-c,uiviewcontroller,core-graphics
Here's what I'm trying to achieve, I've created 3 pictures which do a better job describing what I want to do better than I ever could. Here's what I want to do... I want to keep "App Title" right where it is. I also have a UIImageView now that takes...

Ported OSX Obj-C/CG Screenshot Code to js-ctypes - crash on final line

objective-c,osx,core-graphics,core-foundation,jsctypes
I ported some objective-c/CoreGraphics code that takes a screenshot of multiple monitors but it's not working. The jsctypes guys won't be able to help because the section is just me answering questions lol The objc/cg code is from here: http://stackoverflow.com/a/28247749/1828637 My jsctypes type/struct/declares are here: https://github.com/Noitidart/NativeShot/blob/mac-troubleshooting/modules/ostypes_mac.jsm The jsctypes port is...

Draw/Paint like sketch color inside a bezier path in UIView

ios,objective-c,uiview,core-graphics,uibezierpath
I am drawing a shape layer with help of a UIBezier Path on a UIView : CAShapeLayer *pathLayer = [CAShapeLayer layer]; pathLayer.frame=CGRectMake(-view.frame.origin.x, -view.frame.origin.y, view.frame.size.width, view.frame.size.height); pathLayer.path = path.CGPath; pathLayer.strokeColor = [[UIColor blackColor] CGColor]; pathLayer.fillColor = [[UIColor clearColor] CGColor]; //pathLayer.fillColor = nil; pathLayer.lineWidth = 6.0; pathLayer.lineJoin = kCALineJoinBevel; [view.layer addSublayer:pathLayer]; On...

Resolution Loss when generating an Image from UIImageView

ios,swift,core-graphics
Okay, sorry if the title is a little confusing. Basically I am trying get the image/subviews of the image view and combine them into a single exportable UIImage. Here is my current code, however it has a large resolution loss. func generateImage() -> UIImage{ UIGraphicsBeginImageContext(environmentImageView.frame.size) var context : CGContextRef =...

Shadows Persist in UITableView after reloadSections:

ios,uitableview,cocoa-touch,core-graphics,ios-autolayout
Due to our designer being a sadist, I have a UITableView with a segmented control that switches between two different types of cells in separate feeds. The cells are dequeued with different identifiers and classes — this all works fine. The cells share a parent but are different sizes and...

CoreGraphics random crash

objective-c,core-graphics
This is a little test program to duplicate an intermittent issue in a larger class. The real class creates 4 thumbs of various sizes. This main.m program will crash 1 out of 5 times it's run with EXC_BAD_ACCESS and highlights CGImageRelease(imgRef); If i comment out CGImageRelease(imgRef) then the app experiences...

Converting CIImage to CGImage too slow

ios,xcode,core-graphics,cgimage,ciimage
I need to convert a CIImage to a CGImage. This is the code I am currently using: CGImageRef img = [myContext createCGImage:ciImage fromRect:[ciImage extent]]; But his line of code is very slow for images with a regular size. Much to slow that I can use it properly. Is there another...

Change CGSize with a variable

swift,core-graphics,skaction
I want to change the size of my SKNode with a variable so it can be made smaller with a for loop. It is giving me an error: Cannot find an initializer for type 'CGSize' that accepts an argument list of type '(width:... , height:...)' for (var i = 0.9;...

CGImageCreateWithMaskingColors Does Not Mask on Apple Watch in WKInterfaceImage

ios,core-graphics,watchkit,cgimage,wkinterfaceimage
I've long since had a snippet of code in my iPhone app that attempts to mask the white background from some JPEG images. Today, this message still works well on iPhone, but does not mask whatsoever on Apple Watch. The code runs on the expected successful (non-NULL) path, but no...

Recreate Apple Watch fitness tracker ‘progress’ bar - gradient on CAShapeLayer stroke

core-graphics,cashapelayer
I'm writing an app that could make good use of the Apple Watch's fitness tracker design, here: So far, I've created the basic outline which is just a CAShapeLayer with a CGPath of an ellipse. I use strokeStart and strokeEnd to animate the progress. My problem comes when applying a...

iOS how to remove thin border line then use layer.cornerRadius

ios,core-graphics,layer,rounded-corners
I always use simple method to get view rounded corners + (void)setRoundedCornersByView:(UIView*) givenView roundAngle:(int)roundAngle borderWidth:(double)borderWidth borderColor:(UIColor*)borderColor alphaBorder:(double)alphaBorder { givenView.layer.cornerRadius = roundAngle; givenView.layer.borderColor = [[borderColor colorWithAlphaComponent:alphaBorder] CGColor]; givenView.layer.borderWidth = borderWidth; givenView.layer.masksToBounds = YES; } But now i got thin border around rounded line, it's a thin line which have color like...

How to draw a UIImage rectangle with one or more 100% translucent circles?

ios,uiimage,core-graphics
End Goal I'm doing a tooltip feature for one of my views. The tooltip view will be presented in a modal fashion over the view it's giving tips on. Most of the view will be a dark translucent background, except for one or two key points that need to be...

Vertical Text Alignment in Core Text

ios,objective-c,core-graphics,core-text,core-foundation
EDIT: Here's a Github Gist of the below code if that is more readable. I'm writing a method to draw text within a rectangle in a PDF using Core Text. What I've written accomplishes everything I need, except vertical alignment (top, center, bottom). The standard way that the text is...

Image created from a color is not added to the view?

ios,swift,core-graphics
I try to create an image from a color ( tutorial ) but it does not work for me : this code does not show any image: import UIKit class ViewController: UIViewController { override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let image = ViewController.imageFromColor(UIColor.redColor(), size: CGSizeMake(320,49), radius: 0) println("image \(image.size)") let...

UIBezierPath circle get specific points

ios,uikit,core-graphics,uibezierpath
I would like to draw one big circle and place some smaller circles as shown in the image below I draw the big circe in - (void)drawRect:(CGRect)rect CGFloat rectX = self.frame.size.width / 2; CGFloat rectY = self.frame.size.height / 2; CGFloat width = self.frame.size.width-30; CGFloat height = self.frame.size.width -30; CGFloat centerX...

How to hardcode size attributes of UIView in Swift

ios,swift,uiview,core-graphics
I subclassed a UIView as a bar (making a bar graph), how do I hardcode the size attributes for my FirstBarView? I get the error: FirstBarView type does not have a member named frame? import UIKit @IBDesignable class FirstBarView: UIView { @IBInspectable var FirstBarViewColor: UIColor = UIColor.orangeColor() override func drawRect(rect:...

CAShapeLayer strokeColor from image pattern UIColor to CGColor upside down

ios,objective-c,core-graphics,calayer,cashapelayer
I'm using CABasicAnimation to draw all UIBezierPaths i have in an Array using CAShapeLayer CAShapeLayer *shapeLayer = [CAShapeLayer layer]; shapeLayer.path = [path CGPath]; shapeLayer.strokeColor = path.color.CGColor; My path.color is a UIColor from image pattern. When it is converted to CGColor the image pattern is upside down. I know that is...

Subview added but lost part of drawing?

ios,uiview,uibutton,core-graphics,drawrect
I want to draw a custom UIButton with 'add' surface and UIBlurEffect background. I override the drawRect method in the surface view: class SurfaceAdd: UIView { override func drawRect(rect: CGRect) { var currentContext = UIGraphicsGetCurrentContext() CGContextSaveGState(currentContext) //draw circle CGContextAddEllipseInRect(currentContext, self.bounds) CGContextSetRGBFillColor(currentContext, 0, 0, 0, 0.2) CGContextFillPath(currentContext) CGContextRestoreGState(currentContext) CGContextSetLineCap(currentContext, kCGLineCapRound)...

How to draw a triangle shaped UIButton

ios,uibutton,core-graphics,cgcontext
I really dig the IFTTT bottom right corner button, as shown in the image (bottom right). I tried playing around with CGContext and UIBezierPath to match the effect, but i simply don't know enough to get this right. Does anyone have some thoughts/code on how to make this happen? Thanks!...

How does CALayer geometryFlipped behave exactly?

ios,osx,core-graphics,core-animation,cgaffinetransform
Let's assume i have a CGPathRef called somePath and I'm doing this: CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init]; shapeLayer.path = somePath; shapeLayer.geometryFlipped = YES; But for some reason i don't want to flip the geometry – i'd want to do something like this: CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init]; CGAffineTransform...

iOS freehand drawing with orientation support

ios,core-graphics
I'm using this (https://github.com/acerbetti/ACEDrawingView) sample code in my freehand drawing application. I'm having a problem with orientation changes. Suppose if i draw a circle in portrait and change the orientation to landscape the circle, changes to oval and similarly square changes to rectangle. This happens since, the drawing is rendered...

Drawing in UIView on touch points gives blurr drawing effect on boundaries

ios,objective-c,uiview,core-graphics,calayer
To sort out my last problem : Draw/Paint like sketch color inside a bezier path in UIView, I took an exact Image (of path) filled with selected color. Then, to draw that in UIView, for context color I took: lineColor=[UIColor colorWithPatternImage:img]; in drawrect : CGPoint mid1 = midPoint(previousPoint1, previousPoint2); CGPoint...

Do I need to call 'CGPathRelease' manually in OC?

objective-c,core-graphics,cgpath
Suppose that I get the current context in 'drawRect:rect' method and create a path. What will this path be after calling 'CGContextDrawPath' method? Will it be released safely or cause memory leak, or just remain the same? Do I need to call 'CGPathRelease' manually CGContextRef context = UIGraphicsGetCurrentContext(); CGMutablePathRef path...

CGContextSetShadowWithColor puts shadows on top of the text casting the shadow

ios,text,core-graphics,shadow
I am using CGContextSetShadowWithColor and core graphics to draw shadowed text. The shadow appears, but it also seems to "muddy" the actual text itself which is casting the shadow (which should be pure white). It is as if it is casting the shadow on top of the text (but not...

CoreGraphics - Draw stroke of arc

ios,swift,core-graphics
I have a UIBezierPath which is an arc. I only want to draw the stroke to the graphics context (that is, the stroke of the arc, NOT including the line connecting the end and start points of the arc) Once I’ve done that, I can then add a linear gradient...

: CGContextSetFillColorWithColor: invalid context 0x0

ios,objective-c,core-graphics
I am new to objective-c and now I faced an issue which I don't understand how to solve. I've read this topic CGContextSetFillColorWithColor: invalid context 0x0 but didn't find an answer. The problem is that I get 2 errors like this one: CGContextSetFillColorWithColor: invalid context 0x0. This is a serious...

CITransform vs CATransforms which and when to use?

ios,core-graphics,core-animation,core-image
I need to draw an overlay over camera, which on every frame draws an arrow with a different direction. Now as I understand, because of processing every frame, I should be using the Core graphics drawing, but besides just drawing the arrow I need to give it a perspective effect...

how to add a triangle to UILabel to make it look like a tooltip?

ios,objective-c,uilabel,core-graphics
I want to make a tooltip like UILabel with some custom code. I managed to add a triangle to the UILabel, but since it's out of the bound (or frame) of the UILabel itself. It's not drawing on the screen. Here's my approach: - (void) addTriangleTipToLayer: (CALayer *) targetLayer{ [targetLayer...

What is the purpose of CGFloat [duplicate]

ios,objective-c,core-graphics,cgfloat
This question already has an answer here: Why is CGFloat float on 32 bit and double on 64 bit? 2 answers What makes CGFloat different from a standard float, and why is the CG version needed when working with iOS UI? One thing that bothers me about iOS development...

Array of CGColorRef objects

ios,arrays,core-graphics
I'm trying to create a gradient texture in SpriteKit. To do that, I have to use a CAGradientLayer and render an image. My problem is with setting the colors. From Apple's Documentation: An array of CGColorRef objects defining the color of each gradient stop. Animatable. So in code, I tried...

Draw with core graphics only inside the circle

ios,objective-c,core-graphics
I need to implement a XY dynamic plot - which is drawing in realtime. I have two methods, first of all I draw the circle with fill color: - (void) drawCircleWithFillColorComponent:(float)value { CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2); CGFloat radius = MIN(center.x, center.y)-5.f; UIBezierPath *path = [self...

iOS: How to draw a speical shape

ios,core-graphics,uibezierpath
How to draw the shape shown below using UIBezierPath or CG? I can think of several ways, but none are very good. It contains a small arrow/triangle and a circle. Also I need to be able to rotate this shape, so the angle of the arrow can not be hard...

Take a snapshot of view without adding it to the screen

ios,uiview,uiimage,core-graphics,loadnibnamed
I'm trying to load a view from a nib, configure it, and take a snapshot of it, without adding it to the screen: + (void)composeFromNibWithImage:(UIImage*)catImage completion:(CompletionBlock)completion { NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"CatNib" owner:nil options:nil]; CatView *catView = [nibContents firstObject]; //here, catView is the correct size from the nib, but...

CGBitmapContextCreate: unsupported parameter combination. How to pass kCGImageAlphaNoneSkipFirst [duplicate]

ios,swift,core-graphics
This question already has an answer here: kCGImageAlphaNone unresolved identifier in swift 2 answers I originally wrote this App (GitHub) in Obj-C, but need to convert it to Swift. Upon converting I've been having trouble getting the Context for the Bitmap created. Error Message: Whiteboard[2833] <Error>: CGBitmapContextCreate: unsupported parameter...

How to dealloc CGPoint, CGRect variables?

objective-c,memory,core-graphics,automatic-ref-counting,dealloc
I'm using Objective-C. I use automatic-ref-counting. I have a lot of CGPoint and CGRect variables in my code. I get memory errors. How can I dealloc after used.

Getting a CGContextRef from an NSImage (without using the deprecated graphicsPort)

osx,core-graphics,nsimage
I want to draw into an NSImage using CoreGraphics. I know I could get the image's bitmap data and create a new CGBitmapContext, draw to it and make a new image. thing is: I have a NSImage and im lazy :D In the past I just did: [image lockFocus]; CGContextRef...

How to print UIView.transform on live lldb console

ios,objective-c,core-graphics
Tried:po (CGAffineTransform)[pinch.view transform] but saying error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x20). The process has been returned to the state before expression evaluation. however (lldb) po (CGRect)[pinch.view frame] (origin = (x = 16, y = 96), size = (width = 343, height = 500)) (origin = (x = 16,...