简体   繁体   中英

How to Rotate Image in iPhone?

I am trying to rotate an image in a iPhone application, but I am not able to rotate it for a particular angle which I need. Can any one help in solving this problem?

#import <UIKit/UIKit.h>

@interface ConsumingWebServicesViewController : UIViewController<UITextFieldDelegate>{
    IBOutlet UITextField *username;
    IBOutlet UITextField *password;
    IBOutlet UITextField *deviceid;
IBOutlet    UIActivityIndicatorView *activityindicator;

    NSURLConnection *conn;
    NSMutableData *webData;
}
@property(nonatomic,retain)UITextField *username;

@property(nonatomic,retain)UITextField *password;

@property(nonatomic,retain)UITextField *deviceid;
@property(nonatomic,retain)UIActivityIndicatorView *activityindicator;
-(IBAction)buttonpressed:(id)sender;

@end

To rotate any UIImageView, if that's what you're asking can be done by applying a CGAffineTransformMakeRotation to the image's transform property.

Example

float angleInDegrees = 90; // For example
float angleInRadians = angleInDegrees * (M_PI/180);
myImageView.transform = CGAffineTransformMakeRotation(angleInRadians);

//If someone needs in swift 3

func rotateImageClockWise(theImage: UIImage,imageView:UIImageView) -> UIImage {

    var orient: UIImageOrientation!
    let imgOrientation = theImage.imageOrientation


    UIView.transition(with: imageView, duration: 0.2, options: .transitionCrossDissolve, animations: {() -> Void in

        switch imgOrientation {

        case .left:
            orient = .up

        case .right:
            orient = .down

        case .up:
            orient = .right

        case .down:
            orient = .left

        case .upMirrored:
            orient = .rightMirrored

        case .downMirrored:
            orient = .leftMirrored

        case .leftMirrored:
            orient = .upMirrored

        case .rightMirrored:
            orient = .downMirrored
        }


    }, completion: { _ in })

    let rotatedImage = UIImage(cgImage: theImage.cgImage!, scale: 1.0, orientation: orient)
    return rotatedImage
}

//Just call the method like this :

rotateImageClockWise(theImage: UIImage,imageView:UIImageView)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM