Rotate image

In xcode I have a UIImageView with an image.  When I rotate the image via CGAffineTransformMakeRotation the image not only rotates but the UIImageView changes height and width.
I wish to maintain the height and width.
Any thoughts?
Sheldon LivingstonConsultantAsked:
Who is Participating?
 
mad_macConnect With a Mentor Commented:
you cannot rotate the UIImage directly in that way.

i think what you will need to do is create a new CALayer and add this to the layer tree and rotate the CALayer not the entire view...  a code fragment below which may help

CALayer *imageLayer = [CALayer layer];

imageLayer.position=CGPointMake(50.0f,50.0f);
imageLayer.bounds=CGRectMake(0.0f,0.0f,100.0f,100.0f);
 
imageLayer.contents=[[UIImage imageNamed=@"YOURIMAGE"] CGImage];

[view.layer addSublayer:imageLayer];

imageLayer. affineTransform = CGAffineTransformMakeRotation(-M_PI / 4);

Open in new window


hope this helps, you may need to check the code as i have done this blind.  not in front of a development machine at the moment.
0
 
jschristian44Commented:
it only does that because it doesn't need to keep the aspect ratio.  i am not familiar with the coding in apple at all, so just use a code that puts aspect ratio = 1 or something like that.  that will turn on aspect ration and it should be the right size without changing.  or maybe it's the other way, that aspect needs to be turned off, i am not really sure, try them both.
0
 
Sheldon LivingstonConsultantAuthor Commented:
The line of code that I am using is:
whichAntt.transform = CGAffineTransformMakeRotation(-M_PI / 4);
This has nothing to do with Portrait or Landscape mode... just want to rotate a picture.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jschristian44Commented:
then i suggest using a code to put in the amount of pixels you want it after the rotate code.  like 50 pixels high and 30 pixels wide.  whatever you want to keep it.  simple as that.
0
 
mad_macCommented:
try setting the autoresizingMask of your UIImageView to  UIViewAutoresizingNone which suppresses the auto resizing of a view.
0
 
Sheldon LivingstonConsultantAuthor Commented:
That didn't work mad_mac:.  Inside of this UIImageView I have a UIImage.  How would I just rotate a UIImage instead of the view?
0
 
Sheldon LivingstonConsultantAuthor Commented:
I wonder if I am better off just changing the image...   I basically rotate in 8 points (N, NW, W, etc).  Do you think swapping images would be better than rotating?
0
 
mad_macCommented:
that would depend on what you are trying to achieve here, if you are looking for a smooth animated rotation of the image to one of 8 positions then switching the image will not work.  if it simply jups to one of 8 positions then switching the image will work.

I would give the layer idea a go first
0
 
Sheldon LivingstonConsultantAuthor Commented:
I'd like to try the CALayer approach...  

I currently have a UIImageView that has, or contains, a UIImage.

Does the CALayer sit on top of the view and contain the image?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.