3 回答

TA貢獻1829條經(jīng)驗 獲得超7個贊
import Darwin
class func rotateCameraImageToProperOrientation(imageSource : UIImage, maxResolution : CGFloat) -> UIImage {
? ? let imgRef = imageSource.CGImage;
? ? let width = CGFloat(CGImageGetWidth(imgRef));
? ? let height = CGFloat(CGImageGetHeight(imgRef));
? ? var bounds = CGRectMake(0, 0, width, height)
? ? var scaleRatio : CGFloat = 1
? ? if (width > maxResolution || height > maxResolution) {
? ? ? ? scaleRatio = min(maxResolution / bounds.size.width, maxResolution / bounds.size.height)
? ? ? ? bounds.size.height = bounds.size.height * scaleRatio
? ? ? ? bounds.size.width = bounds.size.width * scaleRatio
? ? }
? ? var transform = CGAffineTransformIdentity
? ? let orient = imageSource.imageOrientation
? ? let imageSize = CGSizeMake(CGFloat(CGImageGetWidth(imgRef)), CGFloat(CGImageGetHeight(imgRef)))
? ? switch(imageSource.imageOrientation) {
? ? case .Up :
? ? ? ? transform = CGAffineTransformIdentity
? ? case .UpMirrored :
? ? ? ? transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
? ? ? ? transform = CGAffineTransformScale(transform, -1.0, 1.0);
? ? case .Down :
? ? ? ? transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
? ? ? ? transform = CGAffineTransformRotate(transform, CGFloat(M_PI));
? ? case .DownMirrored :
? ? ? ? transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
? ? ? ? transform = CGAffineTransformScale(transform, 1.0, -1.0);
? ? case .Left :
? ? ? ? let storedHeight = bounds.size.height
? ? ? ? bounds.size.height = bounds.size.width;
? ? ? ? bounds.size.width = storedHeight;
? ? ? ? transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
? ? ? ? transform = CGAffineTransformRotate(transform, 3.0 * CGFloat(M_PI) / 2.0);
? ? case .LeftMirrored :
? ? ? ? let storedHeight = bounds.size.height
? ? ? ? bounds.size.height = bounds.size.width;
? ? ? ? bounds.size.width = storedHeight;
? ? ? ? transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
? ? ? ? transform = CGAffineTransformScale(transform, -1.0, 1.0);
? ? ? ? transform = CGAffineTransformRotate(transform, 3.0 * CGFloat(M_PI) / 2.0);
? ? case .Right :
? ? ? ? let storedHeight = bounds.size.height
? ? ? ? bounds.size.height = bounds.size.width;
? ? ? ? bounds.size.width = storedHeight;
? ? ? ? transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
? ? ? ? transform = CGAffineTransformRotate(transform, CGFloat(M_PI) / 2.0);
? ? case .RightMirrored :
? ? ? ? let storedHeight = bounds.size.height
? ? ? ? bounds.size.height = bounds.size.width;
? ? ? ? bounds.size.width = storedHeight;
? ? ? ? transform = CGAffineTransformMakeScale(-1.0, 1.0);
? ? ? ? transform = CGAffineTransformRotate(transform, CGFloat(M_PI) / 2.0);
? ? default : ()
? ? }
? ? UIGraphicsBeginImageContext(bounds.size)
? ? let context = UIGraphicsGetCurrentContext()
? ? if orient == .Right || orient == .Left {
? ? ? ? CGContextScaleCTM(context, -scaleRatio, scaleRatio);
? ? ? ? CGContextTranslateCTM(context, -height, 0);
? ? } else {
? ? ? ? CGContextScaleCTM(context, scaleRatio, -scaleRatio);
? ? ? ? CGContextTranslateCTM(context, 0, -height);
? ? }
? ? CGContextConcatCTM(context, transform);
? ? CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
? ? let imageCopy = UIGraphicsGetImageFromCurrentImageContext();
? ? UIGraphicsEndImageContext();
? ? return imageCopy;
}
- 3 回答
- 0 關(guān)注
- 670 瀏覽
添加回答
舉報