Solved

How do I rotate, enlarge UIIMAGES in code?

Posted on 2010-08-14
20
2,298 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article guides you how to create Excel readable XML format using xcode. Here are some points you must take care - First create a NSMutableString variable NSMutableString *excel = [[NSMutableString alloc] init]; and  then build up your …
Let me explain this picture a little bit.  First, in case you haven't already guessed, you are looking at my 2 phones, an Android Samsung Galaxy S5 on the left and an iPhone 5 on the right.  They are on their respective cradles on my desk.  But, you…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

9 Experts available now in Live!

Get 1:1 Help Now