                 completion()
         }
     }
+    
+    func debug() {
+//        println("frame: \(self.imageView.frame) bounds: \(self.imageView.bounds) center: \(self.imageView.center) transform: \(self.imageView.transform.a)")
+    }
+    
+    func adjustBoundsAndTransform(view: UIView) {
+        let center = view.center
+        let scale = view.transform.a
+        view.bounds.size = CGSizeMake(view.bounds.size.width * scale, view.bounds.size.height * scale)
+        view.transform = CGAffineTransformMakeScale(1.0, 1.0)
+        view.center = center
+    }
+    
+    func isImageSmallerThanScreen() -> Bool {
+        let imageWidth = self.imageView.frame.size.width
+        let imageHeight = self.imageView.frame.size.height
+        let screenWidth = self.window.bounds.size.width
+        let screenHeight = self.window.bounds.size.height
+        
+        return imageWidth <= screenWidth && imageHeight <= screenHeight
+    }
+    
+    func adjustImageViewFrame() {
+        if self.isImageSmallerThanScreen() {
+            self.moveImageToCenter()
+            return
+        }
+        
+        let targetView = self.imageView
+        
+        var originX:CGFloat = targetView.frame.origin.x
+        var originY:CGFloat = targetView.frame.origin.y
+        var animateX = true
+        var animateY = true
+        if (targetView.frame.origin.x > 0) {
+            originX = 0
+        } else if (targetView.frame.origin.x < self.window.bounds.width - targetView.bounds.size.width) {
+            originX = self.window.bounds.width - targetView.bounds.size.width
+        }else {
+            animateX = false
+        }
+        if (targetView.bounds.size.height < self.window.bounds.size.height) {
+            originY = (self.window.bounds.size.height - targetView.bounds.size.height)/2
+        } else if targetView.frame.origin.y > 0{
+            originY = 0
+        } else if targetView.frame.origin.y + targetView.bounds.size.height < self.window.bounds.height {
+            originY = self.window.bounds.size.height - targetView.bounds.size.height
+        }
+        else {
+            animateY = false
+        }
+        if animateX || animateY {
+            UIView.animateWithDuration(0.2, animations: { () -> Void in
+                targetView.frame = CGRectMake(originX, originY, targetView.bounds.size.width, targetView.bounds.size.height)
+                }, completion: { (_) -> Void in
+                    
+            })
+        }
+    }
 }

                    }, completion: { (_) -> Void in
                        self.close()
                    })
                } else if targetView.transform.a > 1.0 {
                    // do nothing
                } else {
                    self.moveImageToCenter()
                }
            }
        default:
            _ = 0
        }
    }
    
    func imagePinched(recognizer:UIPinchGestureRecognizer) {

                self.imageView.frame.size = CGSizeMake(width, height)
                self.imageView.center = self.window.center
                }) { (_) -> Void in
                    self.adjustBoundsAndTransform(self.imageView)
            }
        }
    }
