Solved

How do I rotate, enlarge UIIMAGES in code?

Posted on 2010-08-14
20
2,306 Views
Last Modified: 2012-05-10
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?

0
Comment
Question by:beavoid
  • 11
  • 9
20 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 33440659
I know about CGContextRotateCTM function.

Here are few examples:
 
90 degrees (replace 90 with something else and it will work too):
 http://stackoverflow.com/questions/1315251/how-to-rotate-a-uiimage-90-degrees

Find comment with -(UIImage *)resizeImage:(UIImage *)image function here:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/7307-resizing-photo-new-uiimage.html

a clock: http://stackoverflow.com/questions/312226/rotating-two-uiimageviews-360-degrees
0
 

Author Comment

by:beavoid
ID: 33449672
On this page..

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIImage_Class/Reference/Reference.html

What would the line of code look like to flip an image vertically? it isn't clear
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33449738
There is:
imageWithCGImage:scale:orientation:

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

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

typedef enum {
   UIImageOrientationUp,
   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.
0
 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 500 total points
ID: 33449771
Here you will find another approach to rotate the image:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/1164-rotate-uiimage-help.html
0
 

Author Comment

by:beavoid
ID: 33451314
Thanks

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

rotation :

      imageView1.transform = CGAffineTransformMakeRotation(M_PI / 2.0);

enlarge:

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





0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33451709
Read comment 33440659 again.
0
 

Author Comment

by:beavoid
ID: 33458347

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?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33458391
-(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height;

UIImage* image = [self resizeImage: orignalImage width: 48 height: 92];
0
 

Author Comment

by:beavoid
ID: 33459301
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;

	

	

	

	

	

}



@end

Open in new window

0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33459311
It should be after } and before @end.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:beavoid
ID: 33460848
So, all xcode class definitions have data members first in the {}
and  '-' method definitions before  @end
?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33461219
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++).
0
 

Author Comment

by:beavoid
ID: 33465388
With a method indicator + or -

To clarify, do you mean.
static...  as in "exists/callable even if an object doesn't exist
?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33467563
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.
0
 

Author Comment

by:beavoid
ID: 33490037

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)

My..

id path1 = @"http://www.jamescomp.com/pics/runner/runningMan1pixenGIF.gif";
      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?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33490562
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.
0
 

Author Comment

by:beavoid
ID: 33507314
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
0
 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 500 total points
ID: 33507971
I gave the link with example in the beginning:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/1164-rotate-uiimage-help.html

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.
0
 

Author Comment

by:beavoid
ID: 33620251
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);
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33621136
Yes. You knew about the view. You asked about the image here. I didn't know if there is a view somewhere. :(

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now