NSImage Overlay/Mask Gradient like iOS - cocoa

I'm looking to replicate with Cocoa/Core Graphics the process which seems to occur on iOS when setting an image for a UITabBarItem. When the tab bar item is selected, the image is overlayed with a gradient.
For example,
becomes...
I'm unsure exactly what I should be doing to achieve this effect. It seems like the image is masking the gradient. Any pointers in the right direction (or code!) would be much appreciated.

You can use a monochrome CGImage with alpha channel (like most iPhone tool-/tabbar icons) as a mask. Basically, you'd use CGContextClipToMask with your monochrome image. Then you'd draw the gradient which is then clipped to the mask image. You might also want to have a look at the code of UMEKit, which implements this effect on Mac OS X (haven't looked at how exactly they do it, there are probably several ways).

Related

How to show image in a circle shape using C++ builder?

I just want to show images in circle shapes (I use XE8). I tried TCircle. But the only problem I am facing at now is
*) I am having graphics issue (edges are not smooth as it should be), specially when I make the size of TCircle 200px or more.
If I show an already cropped image (in circle shape), it looks much smoother. So I thought, it would be a good idea to crop an image first, then save it as as png image, and then show it on a TImage. If I get solution on TCircle it would be much easier for me, than croping an image. I would appreciate any kind of help or suggestion.

Best way to make a trailing pixel?

So in swift, for iOS devices (though cross platform would be cool if I can still code in Xcode) I am trying to do something rather simple.
All I want to do is have little particles moving around on the screen leaving trails. Unfortunately I cant use traditional particle systems because I need to program their movement.
Anyway I was figuring that if I could keep all the pixels in a large array and just changing them as I need to. That way I can set pixels based on the particle, and just fade them away each frame, thus giving the illusion of a trail.
What would be a good way to do this? I have been looking at quarts, core graphics, and opengles however I cant find a single tutorial that tells me how to draw a single pixel... Just ones that tell how to draw lines and other stuff. I just need to be able to draw the screen pixel by pixel (unless you have a better idea)
What framework should I use
How do I draw a single pixel in it given an x,y screen coordinate (or link a tutorial).
Thanks much!
CoreImage with CIImageAccumulator can help you do this. In my FurrySketch project, I do something very similar. In a nutshell:
Begin a UI Graphics Context: UIGraphicsBeginImageContext(view.frame.size)
Use functions such as CGContextAddLineToPoint to draw to the context
Create a CIImage of the drawing: let drawnImage = UIGraphicsGetImageFromCurrentImageContext()
Use a CISourceOverCompositing to composite that new image over the previous one in the accumulator and write the composite image back to the accumulator:
compositeFilter.setValue(CIImage(image: drawnImage),
forKey: kCIInputImageKey)
compositeFilter.setValue(imageAccumulator.image(),
forKey: kCIInputBackgroundImageKey)
imageAccumulator.setImage(compositeFilter.valueForKey(kCIOutputImageKey) as! CIImage)
Display the new composite in the UI: imageView.image = UIImage(CIImage: imageAccumulator.image())
By also adding a blur to that image, you can get your particles to fade out too, as I've done here.

Cocoa: How to erase line from image by painting?

How to erase line from image (for example CIImage or NSImage or bitmap) by painting it? Just like eraser. I wonder how to do that with Core Graphics?
This is definitely a dupe (I even answered the dupe!) but I cant find it at the moment due to the phone searching being a little fiddly. So, apologies for that, here is the gist of the other answer:
You can do this, assuming your lines are drawn on a separate, equally sized view on top of the image view, by setting the drawing colour to colorWithPatternImage:, using the image view image. Lines drawn with this colour "erase" whatever was previously on the view by effectively drawing small sections of the underlying image on top of your existing drawn lines.

Cocoa / CoreGraphics / Quartz - borderless Quicktime X like window with rounded edges

I am developing a document based application for Mac OS X. It's a kind of media player, but instead of playing audio or video files it is supposed to open text-files containing meta-data specifying OpenGL animations. I would like to mimic Apples QuickTime X window style. This means, i have to do all the window drawings myself, because Cocoa has no appropriate window style.
There is one thing which gives me headaches: The rounded corners usually to be found on Mac OS X windows. I tried using the borderless window mask and working some CGS magic - there are some private Apple headers which allow window shaping, but they are of course undocumented. I was able to cut rectangular holes in my windows edges, but i couldn't figure out how Apple achieves rounded corners.
Creating a transparent window and drawing the frame myself does not work, because an OpenGL viewport is always rectangular, and the only way to change it is to turn on NSOpenGLCPSurfaceOpacity for alpha transparency and using the stencil buffer or shaders to cut out the edges, which seems like a hell of a lot of overhead.
If i put an OpenGLView into a standard Cocoa window with titlebar, the bottom edges are rounded. It seems this is happening at the NSThemeFrame stage of the view hierarchy. Any ideas how this is done?
Use a layer-backed view, and do your drawing in the CALayer on an invisible window. Layers include automatic handling of rounded corners and borders.
Background for CALayer is in the Core Animation Programming Guide. To create a layer for NSView, you need to call [view setWantsLayer:YES]. You would create a CAOpenGLLayer and assign it to the view using setLayer:.
See CALayerEssentials for sample code demonstrating how to use CAOpenGLLayer among other layer types.
Since Robs suggestion didn't work and no one else contributed to the discussion i settled on using the stencil buffer to crop the windows corners. I did this by creating a texture from the windows background and rendering it into the stencil buffer, discarding all transparent pixels. Looks fine, but is slow when resizing the window :/

GDI+ DrawImage() with transparent bitmap to a printer

Does anybody have any pointers on how to successfully draw a bitmap that has
an alpha channel using Graphics::DrawImage() when the Graphics context is
created based on a printer HDC? The printer drivers don't generally support alpha blending - so is there an alternative to rendering everything to an offscreen bitmap and just sending that to the printer. This is often not feasible, especially for high res printing to large format printers.
What kind of printer is that? Regular printers don't print white. Create in-memory image and 'flatten' it (remove alpha channel) and then print the result.
Have you tried drawing a white rectangle to initialize the image before you call the DrawImage method?
The whole point is that I need the line-drawn graphics behind the image to be visible. I did try filling the rectangle first the with RGBA color of (255, 255, 255, 0) but this does not help. Pixels with an alpha value of zero do get printed as fully transparent but partially transparent pixels are drawn fully opaque.
Thanks for asking this question because I was just thinking of perhaps trying to use GDIplus to see whether it could get me around the problems I'm still facing getting patterned diamond shapes to print correctly. Although nowadays alpha-blending does appear to work on most printers, there are still some that draw black corners on the diamonds.
Aside from alpha-blending, I've also tried using diamond-shaped clip regions to surround the shape, but normally the printers that don't support alpha-blending don't seem to support polygonal clip-regions either. I've tried copying from the printer-dc into a bitmap to prime it before drawing the diamond on top, hoping that this will allow me to put back (in the corners) what was there before. This doesn't work either because it appears that the problem boils down to the fact that the printer driver doesn't actually know what is being printed on what part of the page.
In my case, my next plan is to try using a large bitmap brush for drawing the diamond fill directly to the printer hdc. I suspect there's a moderate chance that this too will fail for certain printers. It sounds like it may not be an option for what you were doing.

Resources