diff --git a/SKPhotoBrowser.xcodeproj/project.pbxproj b/SKPhotoBrowser.xcodeproj/project.pbxproj index 1374832..244b608 100644 --- a/SKPhotoBrowser.xcodeproj/project.pbxproj +++ b/SKPhotoBrowser.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 210E53ED1C986D3A008DD5E3 /* UIView+Radius.swift in Sources */ = {isa = PBXBuildFile; fileRef = 210E53EC1C986D3A008DD5E3 /* UIView+Radius.swift */; }; + 210E53EF1C986D57008DD5E3 /* UIImage+Rotation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 210E53EE1C986D57008DD5E3 /* UIImage+Rotation.swift */; }; 8909B5341BC791280060A053 /* SKPhotoBrowser.h in Headers */ = {isa = PBXBuildFile; fileRef = 8909B5331BC791280060A053 /* SKPhotoBrowser.h */; settings = {ATTRIBUTES = (Public, ); }; }; 8909B5431BC791510060A053 /* SKCaptionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8909B53B1BC791510060A053 /* SKCaptionView.swift */; }; 8909B5441BC791510060A053 /* SKDetectingImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8909B53C1BC791510060A053 /* SKDetectingImageView.swift */; }; @@ -19,6 +21,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 210E53EC1C986D3A008DD5E3 /* UIView+Radius.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIView+Radius.swift"; path = "extensions/UIView+Radius.swift"; sourceTree = ""; }; + 210E53EE1C986D57008DD5E3 /* UIImage+Rotation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIImage+Rotation.swift"; path = "extensions/UIImage+Rotation.swift"; sourceTree = ""; }; 8909B5301BC791280060A053 /* SKPhotoBrowser.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SKPhotoBrowser.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 8909B5331BC791280060A053 /* SKPhotoBrowser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SKPhotoBrowser.h; sourceTree = ""; }; 8909B5351BC791280060A053 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -43,6 +47,15 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 210E53EB1C986D1C008DD5E3 /* extensions */ = { + isa = PBXGroup; + children = ( + 210E53EC1C986D3A008DD5E3 /* UIView+Radius.swift */, + 210E53EE1C986D57008DD5E3 /* UIImage+Rotation.swift */, + ); + name = extensions; + sourceTree = ""; + }; 8909B5261BC791280060A053 = { isa = PBXGroup; children = ( @@ -62,6 +75,7 @@ 8909B5321BC791280060A053 /* SKPhotoBrowser */ = { isa = PBXGroup; children = ( + 210E53EB1C986D1C008DD5E3 /* extensions */, 8909B53B1BC791510060A053 /* SKCaptionView.swift */, 8909B53C1BC791510060A053 /* SKDetectingImageView.swift */, 8909B53D1BC791510060A053 /* SKDetectingView.swift */, @@ -175,8 +189,10 @@ files = ( 8909B5441BC791510060A053 /* SKDetectingImageView.swift in Sources */, 8909B54A1BC791510060A053 /* SKZoomingScrollView.swift in Sources */, + 210E53ED1C986D3A008DD5E3 /* UIView+Radius.swift in Sources */, 8909B5471BC791510060A053 /* SKPhoto.swift in Sources */, 8909B5461BC791510060A053 /* SKIndicatorView.swift in Sources */, + 210E53EF1C986D57008DD5E3 /* UIImage+Rotation.swift in Sources */, 8909B5431BC791510060A053 /* SKCaptionView.swift in Sources */, 8909B5491BC791510060A053 /* SKPhotoBrowser.swift in Sources */, 8909B5451BC791510060A053 /* SKDetectingView.swift in Sources */, diff --git a/SKPhotoBrowser/SKPhotoBrowser.swift b/SKPhotoBrowser/SKPhotoBrowser.swift index afc2f45..c5b2087 100644 --- a/SKPhotoBrowser/SKPhotoBrowser.swift +++ b/SKPhotoBrowser/SKPhotoBrowser.swift @@ -710,7 +710,7 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate { senderViewOriginalFrame = (sender.superview?.convertRect(sender.frame, toView:nil))! let imageFromView = senderOriginImage != nil ? senderOriginImage : getImageFromView(sender) - resizableImageView = UIImageView(image: imageFromView) + resizableImageView = UIImageView(image: imageFromView.rotateImageByOrientation()) resizableImageView.frame = senderViewOriginalFrame resizableImageView.clipsToBounds = true resizableImageView.contentMode = .ScaleAspectFill @@ -815,7 +815,7 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate { fadeView.alpha = 1.0 applicationWindow.addSubview(fadeView) - resizableImageView.image = scrollView.photo.underlyingImage + resizableImageView.image = scrollView.photo.underlyingImage.rotateImageByOrientation() resizableImageView.frame = frame resizableImageView.alpha = 1.0 resizableImageView.clipsToBounds = true @@ -1211,17 +1211,3 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate { return statusBarStyle ?? super.preferredStatusBarStyle() } } - -extension UIView -{ - func addCornerRadiusAnimation(from: CGFloat, to: CGFloat, duration: CFTimeInterval) - { - let animation = CABasicAnimation(keyPath:"cornerRadius") - animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) - animation.fromValue = from - animation.toValue = to - animation.duration = duration - self.layer.addAnimation(animation, forKey: "cornerRadius") - self.layer.cornerRadius = to - } -} diff --git a/SKPhotoBrowser/extensions/UIImage+Rotation.swift b/SKPhotoBrowser/extensions/UIImage+Rotation.swift new file mode 100644 index 0000000..80c02bf --- /dev/null +++ b/SKPhotoBrowser/extensions/UIImage+Rotation.swift @@ -0,0 +1,75 @@ +// +// UIImage+Rotation.swift +// SKPhotoBrowser +// +// Created by K Rummler on 15/03/16. +// Copyright © 2016 suzuki_keishi. All rights reserved. +// + +import UIKit + +extension UIImage { + func rotateImageByOrientation() -> UIImage { + // No-op if the orientation is already correct + guard self.imageOrientation != .Up else { + return self + } + + // We need to calculate the proper transformation to make the image upright. + // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. + var transform = CGAffineTransformIdentity; + + switch (self.imageOrientation) { + case .Down, .DownMirrored: + transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height) + transform = CGAffineTransformRotate(transform, CGFloat(M_PI)) + + case .Left, .LeftMirrored: + transform = CGAffineTransformTranslate(transform, self.size.width, 0) + transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2)) + + case .Right, .RightMirrored: + transform = CGAffineTransformTranslate(transform, 0, self.size.height) + transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2)) + + default: + break + } + + switch (self.imageOrientation) { + case .UpMirrored, .DownMirrored: + transform = CGAffineTransformTranslate(transform, self.size.width, 0) + transform = CGAffineTransformScale(transform, -1, 1) + + case .LeftMirrored, .RightMirrored: + transform = CGAffineTransformTranslate(transform, self.size.height, 0) + transform = CGAffineTransformScale(transform, -1, 1) + + default: + break + } + + // Now we draw the underlying CGImage into a new context, applying the transform + // calculated above. + let ctx = CGBitmapContextCreate(nil, Int(self.size.width), Int(self.size.height), + CGImageGetBitsPerComponent(self.CGImage), 0, + CGImageGetColorSpace(self.CGImage), + CGImageGetBitmapInfo(self.CGImage).rawValue) + CGContextConcatCTM(ctx, transform) + switch (self.imageOrientation) { + case .Left, .LeftMirrored, .Right, .RightMirrored: + CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage) + + default: + CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage) + } + + // And now we just create a new UIImage from the drawing context + if let cgImage = CGBitmapContextCreateImage(ctx) { + return UIImage(CGImage: cgImage) + } else { + return self + } + } +} + diff --git a/SKPhotoBrowser/extensions/UIView+Radius.swift b/SKPhotoBrowser/extensions/UIView+Radius.swift new file mode 100644 index 0000000..748dd9d --- /dev/null +++ b/SKPhotoBrowser/extensions/UIView+Radius.swift @@ -0,0 +1,23 @@ +// +// UIView+Radius.swift +// SKPhotoBrowser +// +// Created by K Rummler on 15/03/16. +// Copyright © 2016 suzuki_keishi. All rights reserved. +// + +import UIKit + +extension UIView +{ + func addCornerRadiusAnimation(from: CGFloat, to: CGFloat, duration: CFTimeInterval) + { + let animation = CABasicAnimation(keyPath:"cornerRadius") + animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) + animation.fromValue = from + animation.toValue = to + animation.duration = duration + self.layer.addAnimation(animation, forKey: "cornerRadius") + self.layer.cornerRadius = to + } +}