CGImage represents a drawable image object in Core Graphics (the low-level procedural drawing API for iOS and Mac OS X).
Questions tagged [cgimage]
563 questions
50
votes
12 answers
Convert image to grayscale
I am trying to convert an image into grayscale in the following way:
#define bytesPerPixel 4
#define bitsPerComponent 8
-(unsigned char*) getBytesForImage: (UIImage*)pImage
{
CGImageRef image = [pImage CGImage];
NSUInteger width =…

Ilya Suzdalnitski
- 52,598
- 51
- 134
- 168
44
votes
5 answers
Saving CGImageRef to a png file?
in my Cocoa application, I load a .jpg file from disk, manipulate it. Now it needs to be written to disk as a .png file. How can you do that?
Thanks for your help!
Stefan
34
votes
3 answers
Getting a CGImage from CIImage
I have a UIImage which is loaded from a CIImage with:
tempImage = [UIImage imageWithCIImage:ciImage];
The problem is I need to crop tempImage to a specific CGRect and the only way I know how to do this is by using CGImage.
The problem is that in…

tagyro
- 1,638
- 2
- 21
- 36
29
votes
3 answers
Get pixel data as array from UIImage/CGImage in swift
I have an app so far that allows the user to free draw (like a sketch pad) on a UIImageView element.
I want to get the raw RGB pixel data (as 0 to 255 integer values) as a multidimensional array so I can feed it into a machine learning algorithm .…

Brandon Brown
- 886
- 1
- 9
- 13
27
votes
2 answers
Creating a large GIF with CGImageDestinationFinalize - running out of memory
I'm trying to fix a performance issue when creating GIFs with lots of frames. For example some GIFs could contain > 1200 frames. With my current code I run out of memory. I'm trying to figure out how to solve this; could this be done in batches? My…

klcjr89
- 5,862
- 10
- 58
- 91
26
votes
2 answers
iPhone: Changing CGImageAlphaInfo of CGImage
I have a PNG image that has an unsupported bitmap graphics context pixel format. Whenever I attempt to resize the image, CGBitmapContextCreate() chokes on the unsupported format
I receive the following error (error formatted for easy…

TechZen
- 64,370
- 15
- 118
- 145
24
votes
4 answers
How do I create a UIImage from a CGImage in Swift?
I'm trying to create a UIImage from the ALAssetsGroup#posterImage method. In Objective-C, I could simply call [UIImage imageWithCGImage:group.posterImage] but in Swift, UIImage(CGImage: group.posterImage) gives me a compiler error:
Could not find an…

Bill
- 44,502
- 24
- 122
- 213
22
votes
2 answers
CGImageRelease in Swift
In Objc I use CGImageRelease method after the treatment of an image. But in Swift this method is not available.
On the Apple documentation after Retaining and Releasing Images there is a 2 Objective-C symbols hidden
My question is, why is there no…

Jean Lebrument
- 5,079
- 8
- 33
- 67
22
votes
6 answers
What is the most memory-efficient way of downscaling images on iOS?
In background thread, my application needs to read images from disk, downscale them to the size of screen (1024x768 or 2048x1536) and save them back to disk. Original images are mostly from the Camera Roll but some of them may have larger sizes…

Dan Abramov
- 264,556
- 84
- 409
- 511
21
votes
1 answer
Camera Overlay with User-Photo not saving as edited
I am using a transparent image with a cut out for a user to insert / take their own image. For some reason, while using the UIImagePickerControllerEditedImage and cropping the user-taken photo, the image does not save as it was edited; see photo for…

WrightsCS
- 50,551
- 22
- 134
- 186
19
votes
8 answers
Losing image orientation while converting an image to CGImage
I'm facing an image orientation issue when cropping a square portion of an image out of a rectangular original image. When image is in landscape, it's fine. But when it is in portrait, it seems that the image orientation is not preserved, which…

Robert Brax
- 6,508
- 12
- 40
- 69
19
votes
3 answers
CGImage from byte array
Loading a CGImage or NSImage from a file using a standard image format (jpeg, gif, png et.) is all very simple.
However, I now need to create a CGImage from an array in bytes in memory generated using libfreetype. Its really easy to create OpenGL…

Chris Becke
- 34,244
- 12
- 79
- 148
18
votes
1 answer
Getting RGB pixel data from CGImage
I am trying to access pixel data from a CGImage. I want to be able to access the RGB values as integers. I think I am nearly there with this code:
UIImage* theImage = [UIImage imageNamed:@"rgb.png"];
CGImageRef cgImageRef =…

Rich
- 515
- 1
- 5
- 18
18
votes
2 answers
Pixel Array to UIImage in Swift
I've been trying to figure out how to convert an array of rgb pixel data to a UIImage in Swift.
I'm keeping the rgb data per pixel in a simple struct:
public struct PixelData {
var a: Int
var r: Int
var g: Int
var b: Int
}
I've made my…

austintt
- 485
- 1
- 4
- 13
18
votes
2 answers
JPEG Compression Quality using CGImage
I’m using the CGImage APIs (CGImageDestination, CGBitmapContext,, CGImage etc.) on Mac OS X as well as iOS to compress images to JPEG. Using the kCGImageDestinationLossyCompressionQuality property you can control the compression quality: 0.0 being…

Cutterpillow
- 1,717
- 13
- 32