Results 1 to 5 of 5
Discuss [iPhone SDK] [UIImage CGImage] returning inverted pic at the iPhone Developer Exchange - Hackint0sh.org; i'm trying to get a CGImageRef from a UIImage, but after doing so i get ...
  1. #1
    Newbie Array

    Join Date
    Apr 2008
    Posts
    5
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default [iPhone SDK] [UIImage CGImage] returning inverted pic

    i'm trying to get a CGImageRef from a UIImage, but after doing so i get a verticallly inverted image. any ideas why?
    this is the code

    imageRef = [[UIImage imageNamed:@"photo.jpg"] CGImage]
    CGContextDrawImage(context, bounds, imageRef);



  2. #2
    Newbie Array

    Join Date
    Apr 2008
    Posts
    5
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default

    The CGContext and UIImage are using different coordinate systems. The CGContext has 0,0 as the upper left hand corner and the UIImage has 0,0 as the bottom left hand corner. You can fix this by scaling the CGContext in the Y axis by -1 before drawing the UIImage. There is some historical reason for this, but I really didn't bother to find out why when I ran into this same problem. I use the code below to draw images correctly.

    Code is for SDK Beta 2

    Code:
    // Save the Context State because we want 
    // to restore after we are done so we can draw normally again.
       CGContextSaveGState(context);
    
    // Setup the Context to Invert everything drawn on the Y Axis
       CGContextScaleCTM(context, 1, -1);
    
    // Draw the Image
       CGContextDrawImage(context, bounds, imageRef);
    
    // Restore the Context State so drawing returns back to normal
       CGContextRestoreGState(context);
    Last edited by Hackman; 05-08-2008 at 05:52 PM. Reason: Give notice that sample code is for SDK Beta 2

  3. #3
    Newbie Array

    Join Date
    Apr 2008
    Posts
    5
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default

    thank you man that'll fix it

  4. #4
    Newbie Array

    Join Date
    Apr 2008
    Posts
    5
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default

    I'm currently workin gwith beta 3, i think its bugged because to make it work i had to set a bogus rect to draw in.

    so my code ended up like:

    CGRect bounds = [self bounds];
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Save the Context State because we want
    // to restore after we are done so we can draw normally again.
    CGContextSaveGState(context);

    // Setup the Context to Invert everything drawn on the Y Axis
    CGContextScaleCTM(context, 1, -1);
    bounds.size.height *= -1;

    // Draw the Image
    CGContextDrawImage(context, bounds, imageRef);

    // Restore the Context State so drawing returns back to normal
    CGContextRestoreGState(context);
    bounds.size.height *= -1;

  5. #5
    Newbie Array

    Join Date
    Apr 2008
    Posts
    5
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default

    I'm still working with beta 2 because of the accelerometer / touch problems in beta 3. I think you are right that beta 3 may be bugged because it would be just too strange to want a negative rect size and a inverse scaling transformation. It should be one or the other not both.


 

 

Similar Threads

  1. vmware snow leopard mouse inverted
    By ercev in forum Installation
    Replies: 0
    Last Post: 03-27-2011, 07:24 PM
  2. Jerky and inverted mouse problem.
    By pankirk in forum Installation
    Replies: 5
    Last Post: 10-01-2009, 07:51 PM
  3. UIImage.h
    By The_Kid in forum iPhone Developer Exchange
    Replies: 2
    Last Post: 07-27-2008, 08:54 AM
  4. returning iphone
    By iphone12 in forum General
    Replies: 9
    Last Post: 03-17-2008, 06:19 PM
  5. Returning my iphone.
    By Brandnew70x7 in forum General
    Replies: 5
    Last Post: 09-25-2007, 04:18 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Powered by vBulletin®
Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.
Search Engine Friendly URLs by vBSEO
(c) 2006-2012 Hackint0sh.org
All times are GMT +2. The time now is 05:54 PM.
twitter, follow us!