如何在iPhone中旋转图像?

时间:2011-07-27 07:04:50

标签: iphone

我正在尝试在iPhone应用程序中旋转图像,但我无法将其旋转到我需要的特定角度。任何人都可以帮助解决这个问题吗?

#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

2 个答案:

答案 0 :(得分:3)

要旋转任何UIImageView,如果您要求的话可以通过将CGAffineTransformMakeRotation应用于图像的转换属性来完成。

示例

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

答案 1 :(得分:0)

//如果有人需要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
}

//只需调用这样的方法:

rotateImageClockWise(theImage:UIImage,imageView:UIImageView)