core-graphics

How to get the distance to a CGPath for hit-testing?

橙三吉。 提交于 2021-02-18 12:47:14
问题 I have an open CGPath/UIBezierPath for which I want to detect if the user touches it, i.e. whether a point is within a certain distance from the path. The path is open (i.e. a line/curve, not a shape). It can contain both straight & curved elements. How do I get the distance to the path to hit-test it? 回答1: It seems neither CGPath/UIBezierPath has a function to do this. EDIT: As per @nielsbot's suggestion, you could write a custom implementation using CGPathApply(…) . Computing the distance

Render CoreGraphics to OpenGL texture on IOS

[亡魂溺海] 提交于 2021-02-18 08:19:05
问题 Using CoreGraphics on iOS is very easy to use, but it is possible to get the output of CoreGraphics and put it into OpenGL Textures? The final goal is to use CGContextDrawPDFPage to render very performant pdf's and write it into a specific texture id with OpenGL.glBindTexture(GL_TEXTURE_2D, TextureNativeId); It does look like CoreGraphics is not able to render directly into a specific "native texture id". 回答1: Yes, you can, by rendering your Core Graphics content to a bitmap context and

macOS: simulated mouse event only works when launching binary, not application bundle

大兔子大兔子 提交于 2021-02-16 14:28:05
问题 I have the following code which moves the mouse cursor on a Mac: void moveCursorPos() { CGPoint ppos; ppos.x = 100; ppos.y = 100; CGEventRef e = CGEventCreateMouseEvent(nullptr, kCGEventMouseMoved, ppos, kCGMouseButtonLeft); CGEventPost(kCGHIDEventTap, e); CFRelease(e); } It works when I run my software directly from its binary - e.g. ./foo.app/Contents/MacOS/foo . It does not not work when I start it through open foo.app . Doing open ./foo.app/Contents/MacOS/foo works. The first time I

CGImage Masking stopped working on iOS 12

走远了吗. 提交于 2021-02-09 15:41:20
问题 I've got a method for masking a B&W image by cutting out (i.e. making transparent) any pixels that are above or below a certain brightness. The result would be the same B&W image, but with everything above 70% or below 25% brightness (or whatever you choose) changed to transparent. It was working perfectly on iOS 11, but it broke on iOS 12. It now returns the original, solid image with no modifications every time. -(UIImage*)imageWithLumaMaskFromDark:(CGFloat)lumaFloor toLight:(CGFloat

CGImage Masking stopped working on iOS 12

独自空忆成欢 提交于 2021-02-09 15:41:04
问题 I've got a method for masking a B&W image by cutting out (i.e. making transparent) any pixels that are above or below a certain brightness. The result would be the same B&W image, but with everything above 70% or below 25% brightness (or whatever you choose) changed to transparent. It was working perfectly on iOS 11, but it broke on iOS 12. It now returns the original, solid image with no modifications every time. -(UIImage*)imageWithLumaMaskFromDark:(CGFloat)lumaFloor toLight:(CGFloat

CGImage Masking stopped working on iOS 12

只谈情不闲聊 提交于 2021-02-09 15:40:32
问题 I've got a method for masking a B&W image by cutting out (i.e. making transparent) any pixels that are above or below a certain brightness. The result would be the same B&W image, but with everything above 70% or below 25% brightness (or whatever you choose) changed to transparent. It was working perfectly on iOS 11, but it broke on iOS 12. It now returns the original, solid image with no modifications every time. -(UIImage*)imageWithLumaMaskFromDark:(CGFloat)lumaFloor toLight:(CGFloat

Get random CGPoint on CGPath

柔情痞子 提交于 2021-02-07 09:12:55
问题 I have a CGPath that represents an ellipse. I'd like to place a sprite node at a random point on that path. How can I get a random CGPoint on that CGPath? 回答1: Speaking about a closed CGPath and thinking to a fast method to obtain a random point between infinite points inside a CGPath , first of all I've translated to Swift this: extension CGPath { func forEach(@noescape body: @convention(block) (CGPathElement) -> Void) { typealias Body = @convention(block) (CGPathElement) -> Void func

UIImage Crop Out Transparent Pixels

早过忘川 提交于 2021-02-07 07:32:12
问题 I need help to create perfect clear image after cropping transparent pixel from UIImage using swift 4. I want to create image with remove transparent color, So for that i have used below code. But it blur image with small small square rectangles appears. Using of below code let imageCroppedBg = imgWithClearBackgroung!.cropAlpha() //UIImage extension extension UIImage { func cropAlpha() -> UIImage { let cgImage = self.cgImage!; let width = cgImage.width let height = cgImage.height let

How do I mirror a UIImage picture from UIImagePickerController

十年热恋 提交于 2021-02-06 15:16:25
问题 I'm trying to figure out if there is any way to mirror an image. For example, take a picture of someone's face and then cut it in half and show what their face looks like with each side mirrored. There doesn't seem to be any tricks like this in CGAffineTransform functions. Graphics experts please help!!! 回答1: The basic "trick" here is to use a scaling transform about the X or Y axis with a factor of -1. For example, you could use this to create a "flip about the horizontal axis" transform:

How do I mirror a UIImage picture from UIImagePickerController

孤街醉人 提交于 2021-02-06 15:14:02
问题 I'm trying to figure out if there is any way to mirror an image. For example, take a picture of someone's face and then cut it in half and show what their face looks like with each side mirrored. There doesn't seem to be any tricks like this in CGAffineTransform functions. Graphics experts please help!!! 回答1: The basic "trick" here is to use a scaling transform about the X or Y axis with a factor of -1. For example, you could use this to create a "flip about the horizontal axis" transform: