Questions tagged [cgcontextdrawimage]

CGContextDrawImage is available in OSX and iOS which helps to draws an image into a graphics context. The issues related with drawRect or CGContextDrawImage can be tagged with this tag

CGContextDrawImage draws an image into a graphics context. It can be used by importing Apple's CoreGraphics framework.

There are some use cases (not the majority) for which the CALayer and Core Animation approach will not work. For those cases, CGContextDrawImage could spend a lot of time in decompressing or resampling the images as necessary because the images will not be cached in the layer tree.

For more information:

Example:

Draw your image into a grayscale context

  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
  context = CGBitmapContextCreate(nil, inputWidth, inputHeight,
                     8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaNone);

  CGContextDrawImage(context, imageRect, [imageWithGhost CGImage]);

  CGImageRef imageRef = CGBitmapContextCreateImage(context);
  UIImage * finalImage = [UIImage imageWithCGImage:imageRef];

Related Stack Overflow Questions:

  1. CGContextDrawImage draws image upside down when passed UIImage.CGImage

  2. CGContextDrawImage is EXTREMELY slow after large UIImage drawn into it

  3. rotate image using CGContextDrawImage

74 questions
187
votes
18 answers

CGContextDrawImage draws image upside down when passed UIImage.CGImage

Does anyone know why CGContextDrawImage would be drawing my image upside down? I am loading an image in from my application: UIImage *image = [UIImage imageNamed:@"testImage.png"]; And then simply asking core graphics to draw it to my…
rustyshelf
  • 44,963
  • 37
  • 98
  • 104
33
votes
2 answers

CGContextDrawImage is EXTREMELY slow after large UIImage drawn into it

It seems that CGContextDrawImage(CGContextRef, CGRect, CGImageRef) performs MUCH WORSE when drawing a CGImage that was created by CoreGraphics (i.e. with CGBitmapContextCreateImage) than it does when drawing the CGImage which backs a UIImage. See…
Typewriter
  • 1,216
  • 1
  • 11
  • 18
22
votes
2 answers

Put border around partially transparent Image being drawn on CGContext

I have an image with a yellow vase in the foreground and transparent background: I'm drawing it on a CGContext: CGContextDrawImage(context, CGRectMake(0, 0, 100, 100), myImage.CGImage); I can draw a shadow around it by using the following…
Sanjay Chaudhry
  • 3,181
  • 1
  • 22
  • 31
15
votes
4 answers

Rotate image using CGContextDrawImage

How can I rotate an image drawn by CGContextDrawImage() at its center? In drawRect: CGContextSaveGState(c); rect = CGRectOffset(rect, -rect.size.width / 2, -rect.size.height / 2); CGContextRotateCTM(c, angle); CGContextDrawImage(c, rect,…
9
votes
1 answer

MapView overlay is cutting off after zoom in

I am facing a weird problem with MKMapView. I have used a MKOverlayRenderer. Now the problem is when I am zooming out image showing correctly. But in case of zoom in, some portion of the image are cutting off. It's looking like a portion of MapView…
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
6
votes
2 answers

Replacing UIImageJPEGRepresentation with UIGraphicsImageRenderer's jpegData

I am working to add support for wide color photos in iOS 10. When the user takes a photo from the camera, I need to use the new API that supports the new color space to save the photo data - UIGraphicsImageRenderer's jpegData instead of…
Jordan H
  • 52,571
  • 37
  • 201
  • 351
6
votes
1 answer

How to draw a line between two points over an image in swift 3?

I am new to swift, I want to draw a line between 2 points over an image which I called mapView, I tried to use CGContext but got no result, any idea to help? Thanks. UIGraphicsBeginImageContext(mapView.bounds.size) let context : CGContext =…
4
votes
1 answer

CGPointZero and CGContextDrawImage is unavailable

I couldn't convert CGContextDrawImage for swift 3 , I tried context.draw but it doesn't work. ıs there anyone can give me a feedback Error screenshot Code: import UIKit public struct Pixel { public var value: UInt32 public var red: UInt8 { …
4
votes
0 answers

Extracting image from UIImageView with transform

I wrote a multi-touch based photo tweak program. With UIImageView showing a photo, the transform value is set according to the user touch, to expand/shrink the image. Here's some code that handle the multi-touch. -(void)…
3
votes
0 answers

invalid data bytes/row:CGBitmapContextCreate: CGContextDrawImage: invalid context 0x0

I'm trying to convert array of images into a video file. In the process I have to fill pixel buffer from selected images. Here is the code snippet: CVPixelBufferLockBaseAddress(pixelBuffer, 0) let pixelData =…
3
votes
3 answers

PyGame in MacOSX: CGContextDrawImage: invalid context 0x0

I've recently started using PyGame to develop a simple program that shows an image but I ran into a problem where I can't load images with a lot of colour (?). During my first tests I was using some ground truth segmentation images, so it was all…
bnoite
  • 185
  • 2
  • 12
3
votes
1 answer

How to clear circle in CGContext in iOS

I want create image using CGcontext. This is simple image with white or black background. also I want to add transperent part which is in circle ( check attached image). I know how to do this in rect. But i want to make it circle. Please anyone help…
technerd
  • 14,144
  • 10
  • 61
  • 92
3
votes
1 answer

How to rotate an UIImage but dont cut the edges IOS

Help, Im new to ios programming, I want to rotate may UIImage but I dont want the edges to be cut or loose some part of the image. this is my code: double angle = M_PI * 10/ 180; CGSize s = {image.size.width, image.size.height};…
Alvin John
  • 403
  • 4
  • 14
3
votes
0 answers

CGContextDrawImage -> ImageIO_Malloc leaking

i spent so much time trying to figure out why i get ImageIO leaks in my app...if someone could help me on this one it would be appreciated! I have a UIPageViewController, containing a lot of UIViewControllers with images of 2048x1536. I set the…
AkademiksQc
  • 677
  • 1
  • 6
  • 20
3
votes
1 answer

pdf generated upside down

I am creating pdf using below code UIImage *CurImage=[UIImage imageWithContentsOfFile:[ImageArr objectAtIndex:i]]; UIView *ViewDraw=[[UIView alloc]initWithFrame:CGRectMake(0,10, pageSize, pageSize)]; ViewDraw.backgroundColor=[UIColor…
Heena
  • 2,348
  • 3
  • 32
  • 58
1
2 3 4 5