I have created a CGBitmapContext of 256 x 256 and I have a CGImageRef that I want to draw into this context. My goal is to crop the image (and ultimately create tiles of an image), but if I use CGContextDrawImage, Core Graphics scales the image to fit the context.
So the question is, how do I make sure that only a portion of the CGImageRef is drawn into the CGBitmapContext (no scaling)?
CGContextDrawImage takes a CGRect parameter. It sounds like you are passing the bounds of your context. Instead, try passing the bounds of the image, offset appropriately to draw the desired part of it.
Related
I have a drawing application that draws many lines, polygons etc. on a device context. I'm also drawing background bitmaps that come from an external source and take a long time to load.
When drawing a new frame I first start threads that load the bitmaps, then draw my vector data, and at the end would like to draw the loaded bitmaps while preserving the vector data. I need the bitmaps "under" the vector data but I can't draw them first because they're not loaded, and waiting for them to load would slow things down a lot.
My idea was to apply the "bitmap with transparency" technique:
Copy the portion of my device context that would be covered by
the bitmap into a monochrome image, anything drawn with the
background color should be drawn on, everything else is off-limits.
Copy the image over the bitmap (with the proper ROP code) to mark
what needs to be transparent in the bitmap
StretchBlt the modified bitmap onto the device context
The bitmap needs to be transformed to fit properly on my device context, so I use SetWorldTransform to apply an affine transformation to my device context. The transformation has both a rotation and a shear.
Unfortunately, this fails at step 1 because, as per the documentation of StretchBlt:
If the source transformation has a rotation or shear, an error occurs.
Now, I did try setting the inverse transformation on my monochrome DC, which would transform my sheared data into a proper rectangle, but the function still fails.
So I guess my question is: how do I bitblit a raster image without deleting my data (a function where I give a transparent color in the destination, not the source, would be perfect), OR is there an easy way to extract the color data from a device context that has a rotate and shear transform on it?
I'm looking for some advice on how to proceed.
I'm working on a cocoa program (Objective-C) where I want to be able to draw over top of a bitmap image, defining areas that I can use to get information from the underlying image.
As an example, I'd like to create a box (or oval) and be able to get the average pixel value from the underlying image. Ultimately I want to designate a number of such regions where I am sampling the underlying image to provide various statistics.
Currently I'm using an NSImage class to draw my image but I'm not sure how to go about drawing an NSBezierPath over that image. Would I be better off using something other than NSImage?
Do I simply override the NSImage drawRect method so that it draws a series of NSBezierPath objects?
I would like to be able to save these outlined regions as a layer so that they are available in the future.
You can use a CGBitmapContext (for the bitmap), CGImageMasks (for the masking), and CGPaths or CGContext* drawing primitives for the lines and curves.
A complete answer would be quite long, but that gives you a starting point.
I want to use NSBitmapImageRep to construct a 64x64 pixel sprite in code, and then draw it to the screen, blown up very large. The result would be very large "pixels" on the screen. Think old school Mario Bros. or Minecraft. How can I do this?
Edit I want to draw to this off-screen bitmap and then render it later on a CALayer
Open an new image context with CGBitmapContextCreate and use
void CGContextSetInterpolationQuality (
CGContextRef c,
CGInterpolationQuality quality
);
to set the interpolation quality to kCGInterpolationNone.
Then draw the image into the context.
i have an UIImageView that the user can rotate and resize touching the screen.
I want apply the same changes the user has made on the UIImageView to the UIImage inside it.
Then i will use the UIImage for masking another image.
Please can you explain me what is the correct procedure for doing that?
The main problem is that i can't apply directly the Affine Transformation Matrix of the UIImageView to the [UIImage CGImage], because they use a different coordinate system.
The steps you have to take are:
Create a new graphics context
Apply the transforms from the UIImageView to the context
Draw your original image into the context
Extract a new image from the context
Things you have to watch out for are the inverted co-ordinate system and the fact that the rotated image's bounding rectangle is now larger than the it originally was and you have to take this into account when creating your context.
See my earlier post: Creating a UIImage from a rotated UIImageView
I am taking photos from a webcam in my Cocoa application and I would like to zoom in on the centre of the image I receive. I start by receiving a CIImage and eventually save an NSImage.
How would I go about zooming in on either of these objects?
“Zoom” means a couple of things. You'll need at least to crop the image, and you may want to scale up. Or you may want to reserve scaling for display only.
CGImage
To crop it, use a CICrop filter.
To scale it, use either a CILanczosScaleTransform filter or a CIAffineTransform filter.
To crop and scale it, use both filters. Simply pass the output of the crop as the input of the scale.
NSImage
Crop and scale are the same operation here. You'll need to create a new, empty NSImage of the desired size (whether it's the size of the source crop if you won't zoom or an increased size if you will zoom), lock focus on it, draw the crop rectangle from the source image into the bounding rectangle of the destination image, and unlock focus.
If the destination rectangle is not the same size as the source (crop) rectangle, it will scale; if they are the same size, it will simply copy or composite pixel-to-pixel.