How do I rotate, enlarge UIIMAGES in code?

I have UIIMAGES in my iPhone xcode app.

So I dont have to rotate the images in paint / Gimp
can they be rotated in code? can they be enlarged? what else?

so I can rotate a uiimage to be permanently 90 degrees right? ... twice as big ?
or an arbitrary angle?

Who is Participating?
pgnatyukConnect With a Mentor Commented:
I know about CGContextRotateCTM function.

Here are few examples:
90 degrees (replace 90 with something else and it will work too):

Find comment with -(UIImage *)resizeImage:(UIImage *)image function here:

a clock:
beavoidAuthor Commented:
On this page..

What would the line of code look like to flip an image vertically? it isn't clear
There is:

The orientation of the image data. You can use this parameter to specify any rotation factors applied to the image.

In the bottom:
Specifies the possible orientations of an image.

typedef enum {
   UIImageOrientationDown,   // 180 deg rotation
   UIImageOrientationLeft,   // 90 deg CCW
   UIImageOrientationRight,   // 90 deg CW
   UIImageOrientationUpMirrored,    // as above but image mirrored along
   // other axis. horizontal flip
   UIImageOrientationDownMirrored,  // horizontal flip
   UIImageOrientationLeftMirrored,  // vertical flip
   UIImageOrientationRightMirrored, // vertical flip
} UIImageOrientation;

So    UIImageOrientationLeftMirrored,  UIImageOrientationRightMirrored.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

pgnatyukConnect With a Mentor Commented:
Here you will find another approach to rotate the image:
beavoidAuthor Commented:

This line got rotation stone cold.. on the ImageView, not the image.

rotation :

      imageView1.transform = CGAffineTransformMakeRotation(M_PI / 2.0);


        I couldn't find a transform single line solution like rotation for resizing.
        any suggestions?

Read comment 33440659 again.
beavoidAuthor Commented:

If the implementation of a method is..

-(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height {

What is its corresponding entry in the .h file?

and what would calling it look like?
-(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height;

UIImage* image = [self resizeImage: orignalImage width: 48 height: 92];
beavoidAuthor Commented:
I have your declaration for the method inside the {} of the interface  in  .h

and the error on the method implementation line is

"Expected specifier-qualifier-list before '-' token

My .h is attached

There is nothing before the  -

What could be missing?
//  TestViewController.h
//  Test
//  Created by Joann Crompton on 6/27/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.

#import <UIKit/UIKit.h>

@interface TestViewController : UIViewController {
	-(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height;
	IBOutlet UILabel *label1;
	IBOutlet UILabel *label2;
	UIImageView * firstAnimation;
	UIImageView *imageView1, *ballImageView, *imageView2, *campFireView;
	CGRect myViewFrame;
	CGRect ballViewFrame;
	int XLocation;
	int fingerOffsetX;
	int fingerOffsetY;
	int YLocation;
	int starSizeX;
	int starSizeY;
	UIImage *myImage, *myImage1, *ballImage, *myImage2, *myImage3, *myImage4, *myImage5, *myImage6, *myImage7, *myImage8, *myImage9, *myImage10,*myImage11, *myImage12;
	NSTimer *mainTimer;
	int imgCenterX;
	int timer;
	int lastx;
	int testx, testy, ballx, bally,oldx,oldy;
	//frisbee* frisbees;
	int      animationImagesCount;
	UIImage* animationImages;
	int		 intAnimIndex;


Open in new window

It should be after } and before @end.
beavoidAuthor Commented:
So, all xcode class definitions have data members first in the {}
and  '-' method definitions before  @end
yes. All instance variables are declared between {} in the interface section. All methods are after } and before @end. '-' means that this is an object method. '+' means that this is a class method (the static in C++).
beavoidAuthor Commented:
With a method indicator + or -

To clarify, do you mean.
static...  as in "exists/callable even if an object doesn't exist
I'm not sure I can explain such fundamental things good enough for you.

When you create an object you use alloc, then init. The alloc method is with '+'. You call it for the class. This is the class method. (In C++, it's named as the static method.) You don't need to create an object of the class if you need to call such class method.

'-' is a usual way. When you have an object, you can call its methods. All of them (all public methods). The 'init' is an example of such method. In Objective-C there is a name for them - instance methods - if you have an instance of a class, you call these methods.

It is a very simple explanation. The real story is much longer.
beavoidAuthor Commented:

Do you think the rectangle proportions are expected to be the same? because arbitrary dimensions don't seem to be allowed ( no image gets loaded from the URL)


id path1 = @"";
      NSURL *url1 = [NSURL URLWithString:path1];
      NSData *data1 = [NSData dataWithContentsOfURL:url1];
      myImage1 = [[UIImage alloc] initWithData:data1 cache:NO];

My call is:

      myImage1 = [self resizeImage: myImage1 width: 150 height: 80];

Is 150x80 a bad choice for dimensions?
I think, for a beginning, it's better to see two images: myImage1 and myImage2.
If the current state works for you, leave it as is.

Dimensions is another story that I'm not ready to answer. If this resizeImage works fine, so everything is good. I already forget how the function is written.
beavoidAuthor Commented:
The rotation was solved with this line:

imageView1.transform = CGAffineTransformMakeRotation(M_PI / 2.0);

what would be the same kind of idea be for the size of a UIImage just loaded in?

Not necessarily changing the imageview, maybe the image itself.
For rotation, I changed the Imageview, for size, must I change the image?

How might
I double a UIimage's size?
The above resize code doesn't compile

Thanks All
pgnatyukConnect With a Mentor Commented:
I gave the link with example in the beginning:

Take a look. There are few samples with this function.

I gave few ways to solve the question and asked on additional and new questions you asked. Now I'm leaving this thread. Sorry.

You are very welcome to ask new question.
beavoidAuthor Commented:
for enlargement, the solution is annoyingly simple.

or my viewframe, I simply entered size co-ords 200 x 150 in a new rectangle. It stretches the image to the new size.

      myViewFrame = CGRectMake(testx,testy,200,150);
Yes. You knew about the view. You asked about the image here. I didn't know if there is a view somewhere. :(

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.