Advertisement
iamalizade

ImageCrop

Jan 6th, 2016
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.84 KB | None | 0 0
  1. class ImageCropViewController: UIViewController, UIScrollViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  2.     @IBOutlet var scrollView: UIScrollView?
  3.     var avatarImage: UIImage?
  4.     var avatarImageFrame: CGRect?
  5.    
  6.     var imageView = UIImageView()
  7.  
  8.     @IBOutlet var scrollViewHeightConstraint: NSLayoutConstraint!
  9.     @IBOutlet var scrollViewTopConstraint: NSLayoutConstraint!
  10.     override func viewDidLoad() {
  11.         super.viewDidLoad()
  12.        
  13.         scrollView!.delegate = self
  14.         imageView.frame = CGRectMake(0, 0, (scrollView?.frame.width)!, (scrollView?.frame.height)!)
  15.        
  16.         if let validImage = self.avatarImage {
  17.             self.imageView.image = validImage
  18.             imageView.contentMode = .Center
  19.             imageView.frame = avatarImageFrame!
  20.         }
  21.         imageView.userInteractionEnabled = true
  22.        
  23.         scrollView?.addSubview(imageView)
  24.        
  25.         scrollView?.contentSize = (avatarImage?.size)!
  26.         let scrollViewFrame = scrollView?.frame
  27.         let scaleWidth = (scrollViewFrame?.size.width)! / (scrollView?.contentSize.width)!
  28.         let scaleHeight = (scrollViewFrame?.size.height)! / (scrollView?.contentSize.height)!
  29.        
  30.         let minScale = min(scaleHeight, scaleWidth)
  31.        
  32.         scrollView?.minimumZoomScale = minScale
  33.         scrollView?.maximumZoomScale = 1
  34.         scrollView?.zoomScale = minScale
  35.        
  36.         centerScrollViewContents()
  37.     }
  38.    
  39.     func centerScrollViewContents() {
  40.         let boundsSize = scrollView?.bounds.size
  41.         var contentsFrame = imageView.frame
  42.        
  43.         if contentsFrame.size.width < boundsSize?.width {
  44.             contentsFrame.origin.x = ((boundsSize?.width)! - contentsFrame.size.width) / 2
  45.         } else {
  46.             contentsFrame.origin.x = 0
  47.         }
  48.        
  49.         if contentsFrame.size.height < boundsSize?.height {
  50.             contentsFrame.origin.y = ((boundsSize?.height)! - contentsFrame.size.height) / 2
  51.         } else {
  52.             contentsFrame.origin.y = 0
  53.         }
  54.        
  55.         imageView.frame = contentsFrame
  56.     }
  57.    
  58.     func scrollViewDidZoom(scrollView: UIScrollView) {
  59.         centerScrollViewContents()
  60.     }
  61.    
  62.     func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
  63.         return imageView
  64.     }
  65.    
  66.     @IBAction func cropTheImage(sender: AnyObject) {
  67.         UIGraphicsBeginImageContextWithOptions((scrollView?.bounds.size)!, true, UIScreen.mainScreen().scale)
  68.        
  69.         let offset = scrollView?.contentOffset
  70.         CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -(offset?.x)!, -offset!.y)
  71.         scrollView!.layer.renderInContext(UIGraphicsGetCurrentContext()!)
  72.        
  73.         let image = UIGraphicsGetImageFromCurrentImageContext()
  74.         UIGraphicsEndImageContext()
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement