How to programmatically rotate image by 90 Degrees in iPhone?

60,566

Solution 1

//create rect
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"my_image.png"]];

//set point of rotation
myImageView.center = CGPointMake(100.0, 100.0);

//rotate rect
myImageView.transform = CGAffineTransformMakeRotation(M_PI_2); //rotation in radians

Solution 2

see this:

Rotate image in Quartz? Image is upside down! (iPhone)

 myImage.center = CGPointMake(0, 0);
 myImage.transform = CGAffineTransformMakeRotation([degreesToRadians:imageRotationFix]);

Solution 3

this code will rotate the button..

UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(100, 100, 200, 44)];
btn.transform=CGAffineTransformMakeRotation(M_PI / -4);
[btn setTitle:@"RakeshBhatt" forState:UIControlStateNormal];
[self.view addSubview:btn];

same way u can rotate imageview.

Share:
60,566

Related videos on Youtube

Parth Bhatt
Author by

Parth Bhatt

I am iPhone and iPad developer. https://github.com/akashraje/BidirectionalCollectionViewLayout ^[-_,A-Za-z0-9]$ NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:@"(#[a-zA-Z0-9_-]+)" options:NSRegularExpressionCaseInsensitive error:nil]; NSString *stringData = @"This is Parth known as #parth and this is an awesome place. I am fan of #scganguly."; int count = [reg numberOfMatchesInString:stringData options:0 range:NSMakeRange(0, [stringData length])]; NSLog(@"%d",count); if(count>0) { NSArray *array = [reg matchesInString:stringData options:0 range:NSMakeRange(0, [stringData length])]; NSLog(@"%@",array); NSMutableArray *stringArray = [[NSMutableArray alloc] init]; for (NSTextCheckingResult *result in array) { NSString *stringFinal = [stringData substringWithRange:result.range]; if(stringFinal != nil) { [stringArray addObject:stringFinal]; } } NSLog(@"stringArray: %@",stringArray); } For @user: @"(@[a-zA-Z0-9_]+)" NSLog(@"Request String: %@", requestString); NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]]; // NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"url" ofType:@"plist" ]; // NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc]; // NSString *urlLoc = [fileContents objectForKey:@"baseURL"]; NSString *urlLoc = @"http://portal.abc.com/candidate/post-info"; NSLog(@"URL is %@",urlLoc); NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlLoc]]; NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]]; [request setHTTPMethod: @"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody: requestData]; http://blog.stackoverflow.com/archive/ https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/Introduction/Introduction.html http://nscookbook.com/2013/03/ios-programming-recipe-19-using-core-motion-to-access-gyro-and-accelerometer/ http://code4app.net/category/coremotion http://www.devx.com/wireless/Article/44799 upload audio file to php ios Android post request on IOS https://developers.facebook.com/docs/ios/share https://github.com/oliverbarreto/FastFavs/blob/master/TODO2.h

Updated on July 09, 2022

Comments

  • Parth Bhatt
    Parth Bhatt over 1 year

    Possible Duplicate:
    How to Rotate a UIImage 90 degrees?

    How to programmatically rotate image by 90 Degrees in iPhone?

  • Parth Bhatt
    Parth Bhatt over 12 years
    Almost perfect answer. Just that you gave rotation by 180 degrees and I wanted by 90 degrees but apart from that it is exactly what I needed. Thanks a lot. :)
  • Matoe
    Matoe about 11 years
    Use M_PI instead of 3.14159265, and M_PI_2 instead of M_PI/2