[iPhone] UIImage pixel manipulation

Hello all,
I want to get the pixel data of an UIImage, fiddle with the pixels and create a new UIImage from that modified pixel data. However, I fail. The information provided [here|http://developer.apple.com/qa/qa2007/qa1509.html] doesn't seem to work on iPhones. Somehow the image always has 3 components (even when I created it with an alpha channel) but when I try to create a 24 bit context I get an "unsupported parameter combination" error like:
unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component colorspace; kCGImageAlphaNone; 66 bytes/row.
because it's invalid, according to [this info|http://developer.apple.com/qa/qa2001/qa1037.html]. But when I create an 4 component context the image is all garbeld up and looks like colorful noise. I created a simple test case where I just get the image data and create a new image from that very data. This should return an image that looks exactly like the original, but it fails due to the problems mentioned above. Does anybody have an idea?
Thanks and regards,
Sebastian Mecklenburg
UIImage* manipulateImage (UIImage* inImage)
CGImageRef image = inImage.CGImage;
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
int bitsPerComponent = 8;
// Use the generic RGB color space.
colorSpace = CGColorSpaceCreateDeviceRGB();//CGImageGetColorSpace(image);//
if (colorSpace == NULL) {
fprintf(stderr, "Error allocating color space
return NULL;
int numberOfCompponents = 4;//CGColorSpaceGetNumberOfComponents(colorSpace); //always returns 3 -> unsupported parameter combination
int bitsPerPixel = bitsPerComponent * numberOfCompponents;
size_t pixelsWide = CGImageGetWidth(image);
size_t pixelsHigh = CGImageGetHeight(image);
CGRect imageRect = {{0,0},{pixelsWide, pixelsHigh}};
// Declare the number of bytes per row. Each pixel in the bitmap in this example is represented by 4 bytes; 8 bits each of red, green, blue, and alpha.
bitmapBytesPerRow = (pixelsWide * numberOfCompponents);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL) {
fprintf (stderr, "Memory not allocated!");
CGColorSpaceRelease( colorSpace );
return NULL;
context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh,
bitsPerComponent, bitmapBytesPerRow, colorSpace,
kCGImageAlphaPremultipliedFirst);//kCGImageAlphaNoneSkipFirst);//kCGImageAlphaNone);//
if (context == NULL) {
free (bitmapData);
fprintf (stderr, "Context not created!");
CGColorSpaceRelease( colorSpace );
return NULL;
CGContextDrawImage(context, imageRect, image);
void *data = CGBitmapContextGetData (context);
//do something with data
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, data, bitmapByteCount, NULL);
CGImageRef cgImage = CGImageCreate(pixelsWide, pixelsHigh, bitsPerComponent,
bitsPerPixel, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst,
dataProvider, NULL, false, kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);
CGColorSpaceRelease(colorSpace);
// When finished, release the context
CGContextRelease(context);
// Free image data memory for the context
if (data) {
free(data);
return [UIImage imageWithCGImage:cgImage];

Hello all,
I want to get the pixel data of an UIImage, fiddle with the pixels and create a new UIImage from that modified pixel data. However, I fail. The information provided [here|http://developer.apple.com/qa/qa2007/qa1509.html] doesn't seem to work on iPhones. Somehow the image always has 3 components (even when I created it with an alpha channel) but when I try to create a 24 bit context I get an "unsupported parameter combination" error like:
unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component colorspace; kCGImageAlphaNone; 66 bytes/row.
because it's invalid, according to [this info|http://developer.apple.com/qa/qa2001/qa1037.html]. But when I create an 4 component context the image is all garbeld up and looks like colorful noise. I created a simple test case where I just get the image data and create a new image from that very data. This should return an image that looks exactly like the original, but it fails due to the problems mentioned above. Does anybody have an idea?
Thanks and regards,
Sebastian Mecklenburg
UIImage* manipulateImage (UIImage* inImage)
CGImageRef image = inImage.CGImage;
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
int bitsPerComponent = 8;
// Use the generic RGB color space.
colorSpace = CGColorSpaceCreateDeviceRGB();//CGImageGetColorSpace(image);//
if (colorSpace == NULL) {
fprintf(stderr, "Error allocating color space
return NULL;
int numberOfCompponents = 4;//CGColorSpaceGetNumberOfComponents(colorSpace); //always returns 3 -> unsupported parameter combination
int bitsPerPixel = bitsPerComponent * numberOfCompponents;
size_t pixelsWide = CGImageGetWidth(image);
size_t pixelsHigh = CGImageGetHeight(image);
CGRect imageRect = {{0,0},{pixelsWide, pixelsHigh}};
// Declare the number of bytes per row. Each pixel in the bitmap in this example is represented by 4 bytes; 8 bits each of red, green, blue, and alpha.
bitmapBytesPerRow = (pixelsWide * numberOfCompponents);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL) {
fprintf (stderr, "Memory not allocated!");
CGColorSpaceRelease( colorSpace );
return NULL;
context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh,
bitsPerComponent, bitmapBytesPerRow, colorSpace,
kCGImageAlphaPremultipliedFirst);//kCGImageAlphaNoneSkipFirst);//kCGImageAlphaNone);//
if (context == NULL) {
free (bitmapData);
fprintf (stderr, "Context not created!");
CGColorSpaceRelease( colorSpace );
return NULL;
CGContextDrawImage(context, imageRect, image);
void *data = CGBitmapContextGetData (context);
//do something with data
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, data, bitmapByteCount, NULL);
CGImageRef cgImage = CGImageCreate(pixelsWide, pixelsHigh, bitsPerComponent,
bitsPerPixel, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst,
dataProvider, NULL, false, kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);
CGColorSpaceRelease(colorSpace);
// When finished, release the context
CGContextRelease(context);
// Free image data memory for the context
if (data) {
free(data);
return [UIImage imageWithCGImage:cgImage];

Similar Messages

  • IPhone 5 pixelated images in various apps problem- occurred within last 2 months and must be a result of apple network driver update

    iPhone 5 pixelated images in various apps problem- occurred within last 2 months and must be a result of apple network driver update
    What's the fix?
    Tried reset, restart, reboot, reopen of apps same problem

    What do you mean by reboot? Do you mean restore? Because if you haven't restored, then that's the next step. You'll need a computer with the latest version of iTunes and a USB connector.
    For Mac:
    http://support.apple.com/kb/PH12124
    For PC:
    http://support.apple.com/kb/PH12324

  • The iPhone Dead Pixels

    Hi
    +I own an iPhone 2G 16GB running on OS 3.1.2.+
    +There are some dead pixel on my screen.+
    +Is there any way to fix them softwarely or just I have to replace the screen?+
    +Also is there any idea about the Knox iPhone Stuck Pixel Fix method?+
    +Merry Christmas and the Lord bless+
    Mehran

    The only way to tell if it is software related would be to restore your iphone back to its original settings and choosing "setup as a new iphone" at then end of the process. Other than that its going to be a issue with the display itself and would need to be replaced.

  • IPhone 6 Pixelated images on instagram, pinterest and the icons on the app store

    Hi!!! I have an iPhone 6, 64GB capacity. All the iOS updates are the latest ones. I'm getting pixelated images on instagram, pinterest and even the icons on the app store, I have tried to turn it of and off but it doesn't seems to work... any idea??.

    Hi alex_h1!!!
    i'm posting some of the images that are pixelated, I haven't  noticed but that includes safari too... Here are some screenshots! I thought maybe it was the internet speed but the same thing happens even on wi-fi .
    Thanks!!!

  • Screen issue iPhone 4 - pixel drop

    I have had a replacement iPhone 4 for just over 12 months now (around 27th January last year I had it replaced due to a small crack on the back casing near the camera, was due to the official bumper putting pressure on the camera corner). And recently I have noticed that there is a small mark on the screen when viewing some pages with certain colours. As I bought my original iPhone 4 on release day, I am not covered by AppleCare. I was just wondering if anyone knows if they can do anything for me at the Apple store. The issue is toward the top of the screen and is like a small line of pixels that are fine with deeper colours, but have problems with whites, blues and lighter shades.
    If anyone has any ideas, please let me know. I am not really willing to pay to get this problem solved as I think the price for repair is way too high.
    Thanks.
    Damien.

    i dont think thats what hes talking about if you look closely at the bottom right hand side of the image there is a redish circle that looks burned into the screen and another rainbow looking one just above it
    as to the op I would take your phone into a apple store for replacement seems like there is something wrong with your screen

  • IPhone Dead pixels? or dirt on the screen protector?

    Hello, I have an iPhone 4 that I got on the 29th of May, anyway I discovered this yesterday that when i enter the camera app or watch a youtube video when theres a black screen, or the iPhone is booting i see about 3 white dots on the screen, however my wallpaper is mostly black but I can't manage to find them so this problem only accours when I use the camera app or watch a youtube video and boot up the phone.. It's pretty annoying because I treat this phone like a newborn baby and this is what I get...

    how can I identify stuck pixels with a magnifying glass?
    You can't, but you can identify dust. 
    Time to google:  Solving iphone stuck pixels

  • Apple wont replace my iPhone (dead pixel)

    i just got off the phone with iPhone support and they said they wouldn't replace my phone... I have a dead pixel and the guy said it had to be more then one pixel to "be a problem"
    So my one dead pixel that i notice every time i look at my phone isn't a problem? whats up with that?
    I love apple to death but come on... i spent $600 on this phone and your not gonna help me out?

    It's a tough thing to hear but any company that manufactures an LCD anything will usually tell you that dead pixels are just a part of the hardware and you typically need several for it to be considered defective. This is what you'll get from any MP3 or monitor manufacturer.

  • IPhone dead pixels?

    I recently got my iPhone's screen changed after I noticed a huge amount of dead grey pixels growing near the bottom. After being replaced, it was good as new. A couple of months later and I noticed a few grey pixels at the top and they grew longer and wider by the day. At this point, I can only see half of the time and my carrier. I sent in my iPhone once more and they told me they switched my LCD, and I have payed for it as well as I no longer have a warranty but I can still see the dead pixels near the top. The first time was succesful, why not now? I'm starting to doubt the fact that they changed my LCD. Can anyone please tell me if the pixels would remain even after changing an LCD? It seems highly unlikely. Please help me as I still have the reciept and would need to take action immediately.

    how can I identify stuck pixels with a magnifying glass?
    You can't, but you can identify dust. 
    Time to google:  Solving iphone stuck pixels

  • Dead iPhone 5 pixel

    Hello, I live in the UK and have bought a sim free iPhone 5 from an apple store about 60 days ago. The phone had developed a dead pixel to the mid left of the screen, if I return to the store what can I expect to happen and do I need to take anything other than the phone with me.

    Call and find out.

  • IPhone Dead Pixel

    Hey everyone.  recently got my iphone about three weeks ago and have already noticed a dead pixel. It is a white pixel on a black background, particularly noticeable when charging and booting up. I love my iphone, it works great its just that this dead pixel is kinda frustrating considering the amount of money I spent for this phone.
    I was wondering if apple would replace my iphone?
    And also, how long does the warranty last for dead pixels? Should I return it before the 30 day mark, or will they still replace it up to a year?
    Thanks for your help.

    mdigregori wrote:
    so as long as its under a year, they will replace my phone with a dead pixel?
    Maybe.
    But you will need to take it to an Apple store or go here -> https://selfsolve.apple.com/GetWarranty.do

  • Iphone - Dead pixel policy

    Hi Community I'm from Argentina this is my first post here.
    I have a question, can someone tell me which is the dead pixel policy of Apple? I have a 3G iPhone with 1 pixel dead, and I want to know if the warranty works for my phone.
    Looking forward to your reply.
    Greetings, Manu.

    Try this:
    http://www.apple.com/webapps/utilities/stuckpixelfix.html

  • IPhone 4S pixelated video when msging

    Why does my phone send pixelated video messages? Also most video that is sent to me is pixelated as well. Doesn't seem to matter if its another iPhone, seems to do it no matter what kind of phone. The video is fine on my phone and also if I email it is fine a well. Only when I send it by text. Any advice??

    Carriers set limits on the size of videos that can be sent via MMS, so the video was compressed.
    Take a look at this Apple doc -> iMovie for iOS: Why are videos shared through the Camera Roll lower resolution?
    You can avoid the problem by spliting the video into two parts or just sending smaller videos.

  • NEED HELP FOR PIXEL MANIPULATION

    Hi.. i'm beginner to java.. i want to take each pixel of the image and check the RGB value to find whether that pixel is balck or white?
    i assign 1 for white and 0 for black,i have to manipulate like if i find 10 continues white, then i must need value of array as 101, like this if i find 5 black then 50..
    this is way my code must work.. but its not woking properly..
    can anyone help me out plz?
    i ill post my code here..
    import java.lang.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.Color;
    public class colortest {
        public static BufferedImage createTextImage(String text, Font font) {
            boolean isAntiAliased = true;
            boolean usesFractionalMetrics = false;
            FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics);
            TextLayout layout = new TextLayout(text, font, frc);
            Rectangle2D bounds = layout.getBounds();
            int w = (int) Math.ceil(bounds.getWidth());
            int h = (int) Math.ceil(bounds.getHeight());
            BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); //for example;
            Graphics2D g = image.createGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0,0,w,h);
            g.setColor(Color.BLACK);
            g.setFont(font);
            Object antiAliased = isAntiAliased?
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliased);
            Object fractionalMetrics = usesFractionalMetrics?
                RenderingHints.VALUE_FRACTIONALMETRICS_ON : RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
            g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics);
            g.drawString(text, (float) - bounds.getX(), (float) - bounds.getY());
            g.dispose();
    return image;
      private void marchThroughImage(BufferedImage image) {
    int white=0,black=0,k=0;
    int w = image.getWidth();
    int h = image.getHeight();
    int flag[][]=new int[h][w];
    int[][] alpha = new int[h][w];
    int[][] red = new int[h][w];
    int[][] green = new int[h][w];
    int[][] blue = new int[h][w];  
    int[][] pixel=new int[h][w];
    int[][] scanline=new int[h][w];
       //System.out.println("width, height: " + w + ", " + h);
        for (int i = 0; i < h; i++) {
          for (int j = 0; j < w; j++) {
           System.out.println("i,j: " + i + ", " + j);
            pixel[i][j] = image.getRGB(j, i);
         alpha[i][j] = (pixel[i][j] >> 24) & 0xff;
         red[i][j] = (pixel[i][j] >> 16) & 0xff;
         green[i][j] = (pixel[i][j] >> 8) & 0xff;
         blue[i][j] = (pixel[i][j]) & 0xff;
    System.out.println("argb: " + alpha[i][j]+ ", " + red[i][j] + ", " + green[i][j] + ", " + blue[i][j]);
    System.out.println("");
    try{
       for (int i = 1; i <= h; i++) {
          for ( int j = 1; j <= w; j++) {
    if(red[i][j]==255 && green[i][j]==255 && blue[i][j]==255)
    flag[i][j]=1;
    flag[0]=1;
    else
    flag[i][j]=0;
    flag[i][0]=0;
    if(flag[i][j]==1)
    white++;
    else{
    if(flag[i][j-1]==1){
    scanline[i][k]=1;
    scanline[i][k++]=white;
    white=0;
    k++;
    if(j==w && flag[i][j]==1)
    scanline[i][k]=1;
    scanline[i][k++]=white;
    white=0;
    k++;
    if(flag[i][j]==0)
    black++;
    System.out.println(black);
    else{
    if(flag[i][j-1]==0)
    scanline[i][k]=0;
    scanline[i][k++]=black;
    black=0;
    k++;
    System.out.println(black);
    if(j==w && flag[i][j]==0)
    scanline[i][k]=0;
    scanline[i][k++]=black;
    black=0;
    k++;
    System.out.println(scanline[i][j]);
    catch(Exception e){}
    public colortest() {
    try {
    BufferedImage image = ImageIO.read(this.getClass().getResource("example.bmp"));
    marchThroughImage(image);
    } catch (IOException e) {
    System.err.println(e.getMessage());
    public static void main(String[] args) throws IOException {
    BufferedImage image = createTextImage(".", new Font("Lucida Bright", Font.BOLD,12));
    ImageIO.write(image, "bmp", new File("example.bmp"));
    JLabel label = new JLabel(new ImageIcon(image));
    label.setBorder(BorderFactory.createTitledBorder("Here is the image, bounds brought to you by TextLayout.getBounds"));
    final JFrame f = new JFrame("Example");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cp = f.getContentPane();
    cp.add(label);
    f.pack();
    SwingUtilities.invokeLater(new Runnable(){
    public void run() {
    f.setLocationRelativeTo(null);
    f.setVisible(true);
    new colortest();

    to start off:
    i would try this tutorial:
    http://www.digitalartsonline.co.uk/tutorials/?featureID=3355153
    its a deep "sea" look, but you can alter it for the lighting color you want.
    this as well:
    http://www.digitalartsonline.co.uk/tutorials/?featureID=3340522
    digital arts has some pretty cool tutorials.
    -janelle

  • MovieClip pixel manipulation with BitmapData

    Hi.
    I have a MovieClip in the library exported for actionscript. How do i convert or attachMovie it to a BitmapData class so that i can manipulate its pixel data? I will need that so that i can distort the MovieClip. Please don't tell me to google it coz i did and can't find anything . Just a simple snippet how to do it please?

    use the movieclip attachMovie() method to create a movieclip instance of your library object, and then use the draw() method of the bitmapdata class to create a bitmap of your instance:
    var mc:MovieClip = whatever.attachMovie("yourID","mc",depth)
    var bmp:BitmapData=new BitmapData(mc._width,mc._height);
    bmp.draw(mc);

  • How do I get a new phone from my warrenty because my current iPhone has a pixel issue?

    My current iphone has pixel that bothers me while im watching videos, playing games etc. I cant see the pixel when the whole screen is lit up but when majority of the screen is lit up and the top and bottom parts arnt I can see the pixel always going white all the time,

    If it is only one pixel you are unlikely to get a replacement. The industry norm is five or six  stuck pixels before a replacement will be considered.
    No harm in going to the Apple store and asking them nicely though... it sometimes works!

Maybe you are looking for

  • My ipad has been stolen or lost. How to track it down?

    I don't have the Find Me app and the mobile me locator cannot find it. Is there any way to find its location with the serial number or any other way? Anything helps. Man this *****.

  • [svn] 3768: Fix BLZ-262: null pointer exception on redeploy of web app/ servlet.

    Revision: 3768 Author: [email protected] Date: 2008-10-21 07:48:14 -0700 (Tue, 21 Oct 2008) Log Message: Fix BLZ-262: null pointer exception on redeploy of web app/servlet. I was too agressive in releasing the ThreadLocal variables when the servlet s

  • Help with Size Issues

    Hi I am rather new to DVDSP, and I have a q.t. file that I importorted as an asset and it says it 14 gb. it is a 1hr 14 min HDV file that I exported from FCP to compressor. Can I get it on one DL 8.0 DVD? How ? Thanks T.

  • Audit Vault  Agent sizing (harware requirements)

    Hi, I need to provide my customer an estimation of the disk space needed for the Audit Vault Agent, size of archivelog files of the source database so that REDO collector could work without problems, and other demands of source side (audit vault agen

  • Battery fine (?), won't run macbook

    Hey there Fairly new (just out of warranty) MacBook. Battery is charged. Battery works fine in an older MacBook. However, on this laptop disconnecting the power adapter results in instant power off. Using the battery from another MacBook does not cha