transformed image onto the canvas

Discussion in 'iOS Programming' started by KardelSharpeye, May 22, 2009.

  1. KardelSharpeye macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #1
    Good morning friends,

    I was just wondering if there is anyway to draw an image with its transform values into CGContext. basically i have the following code.

    Code:
    // Size of the new image.
    CGSize size = CGSizeMake(width, height);
    
    // Context.
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // Your images here.
    UIImage *image1;
    UIImage *image2;
    
    // Draw them to the context.
    [image1 drawInRect:CGRectMake(x, y, width, height)];
    [image2 drawInRect:CGRectMake(x, y, width, height)];
    
    // Construct the image from the context and return it.
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage; // The thing you want.
    
    but after looking at the image all the previous rotation is gone. scaling however works which is kinda strange.

    any help is greatly appreciated.
     
  2. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #2
    I don't see too much rotation going on. I see you getting the current graphics context and then immediately drawing the images.

    Where are you making the rotations happen?
     
  3. KardelSharpeye thread starter macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #3
    sorry this is my save image only.

    Code:
    // Size of the new image.
    CGSize size = CGSizeMake(width, height);
    
    // Context.
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // Your images here.
    UIImage *image1 = imageSelected1.image;
    UIImage *image2 = imageSelected2.image;
    
    // Draw them to the context.
    [image1 drawInRect:CGRectMake(x, y, width, height)];
    [image2 drawInRect:CGRectMake(x, y, width, height)];
    
    // Construct the image from the context and return it.
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage; // The thing you want.
    
    and i have:
    Code:
    UIImageView imageSelected1, imageSelected2;
    
    imageSelected1.transform = CGAffineTransformRotate( imageSelected1.transform, degreesToRadians(3));
    
    imageSelected2.transform = CGAffineTransformRotate( imageSelected2.transform, degreesToRadians(3));
    
    in main which is called before that save function
     
  4. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #4
    I don't have too much experience with that transform property; I would just do something like:

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextRotateCTM(context, angle);

    [image1 drawInRect:CGRectMake(x, y, width, height)];
    [image2 drawInRect:CGRectMake(x, y, width, height)];

    Which should work. Now that I started working with Core Graphics more, I found myself dropping down instead of using the UIKit additions. If you can find a way to make the rotation work without the CGContextRotateCTM function call, that might be ideal.
     
  5. KardelSharpeye thread starter macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #5
    omg thank you so much that worked!!! yeah there is so much for me to learn right now because i only have been into this for less than a month. but yes thank you again!
     
  6. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #6
    Now that I think about it, you were originally applying a transformation to the *view* -- this shouldn't have affected the *images*, which is why you were not seeing the rotations.
     
  7. KardelSharpeye thread starter macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #7
    yeh but UIImageView is the only one that has transform property how else can i rotate an image? also now when i save it my coordinates is a bit off. any idea why this is happenning? i was thinkng bout using convertPoint:toView but it doesnt make sense because i am actually using the image coords...in the currentview(self)...
     
  8. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #8
    What happens if, instead of transforming the context and then drawing, you draw and *then* transform the context? Does that make any difference?
     
  9. KardelSharpeye thread starter macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #9
    image will not rotate if i transform the context AFTER i drawInRect...also it seems that the more angle i apply to the context the further the image move away from the source when is it drawn in the context. and when i move the angle back to its original angle or a full 360degrees. the image saves like it normoally would....
     
  10. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #10
    Like I said, I'm don't use the UIKit drawing functions, too much, but my guess is that you might want to try re-evaluating the size parameter that you are passing to the bitmap graphics context. You might want to make the size large enough to contain the whole rotated image, if you know what I mean. I'm not sure if the UIKit function will crop or resize if the size is too small. This might help you position the origin better.

    You can also apply another transform to the rotated image to bring the origin of the image back where you want it.
     

Share This Page