Advertisement
Guest User

Cropping image

a guest
Feb 14th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.82 KB | None | 0 0
  1. import UIKit
  2.  
  3. class AdvancedCroppingViewController: UIViewController {
  4.     var imageContainer:UIView!
  5.     var image:UIImage!
  6.     var topImage:UIImage!
  7.     var imageView:ZoomRotatePanImageView!
  8.     var topImageView:UIImageView!
  9.     var fillerView1:UIView!
  10.     var fillerView2:UIView!
  11.     var centerPoint:CGPoint!
  12.     var tw:CGFloat!
  13.     var th:CGFloat!
  14.     var tx:CGFloat!
  15.     var ty:CGFloat!
  16.     var iw:CGFloat!
  17.     var ih:CGFloat!
  18.     var ix:CGFloat!
  19.     var iy:CGFloat!
  20.     var iratio:CGFloat!
  21.    
  22.     override func viewDidLoad() {
  23.         super.viewDidLoad()
  24.         loadImages()
  25.     }
  26.  
  27.     func loadImages(){
  28.         topImage = UIImage(named: topImageName)
  29.         topImageView = UIImageView(image: topImage)
  30.         imageView = ZoomRotatePanImageView(image: image)
  31.        
  32.         let w = self.view.frame.width
  33.         let h = self.view.frame.height
  34.        
  35.         //TOP IMAGE
  36.        
  37.         let ratioW:CGFloat = w / topImage.size.width
  38.         let ratioH:CGFloat = h / topImage.size.height
  39.         let ratio:CGFloat = ratioW < ratioH ? ratioW : ratioH //Aspect Fit
  40.        
  41.         tw = topImage.size.width * ratio
  42.         th = topImage.size.height * ratio
  43.         tx = (w - tw) / 2.0
  44.         ty = (h - th) / 2.0
  45.        
  46.         topImageView.frame = CGRectMake(tx, ty, tw, th)
  47.        
  48.         //FILLER VIEWS
  49.        
  50.         if(tx > 0){
  51.             fillerView1 = UIView(frame: CGRectMake(0, 0, tx, th))
  52.             fillerView2 = UIView(frame: CGRectMake(w - tx, 0, tx, th))
  53.         }
  54.         else{
  55.             fillerView1 = UIView(frame: CGRectMake(0, 0, w, ty))
  56.             fillerView2 = UIView(frame: CGRectMake(0, h - ty, w, ty))
  57.         }
  58.        
  59.         fillerView1.backgroundColor = UIColor.whiteColor()
  60.         fillerView2.backgroundColor = UIColor.whiteColor()
  61.        
  62.         //IMAGE
  63.        
  64.         imageContainer = UIView(frame: CGRectMake(0, 0, w, h))
  65.         imageContainer.backgroundColor = UIColor.whiteColor()
  66.        
  67.         let iratioW:CGFloat = w / image.size.width
  68.         let iratioH:CGFloat = h / image.size.height
  69.         iratio = iratioW > iratioH ? iratioW : iratioH //Aspect Fill
  70.        
  71.         iw = image.size.width * iratio
  72.         ih = image.size.height * iratio
  73.         ix = (w - iw) / 2.0
  74.         iy = (h - ih) / 2.0
  75.        
  76.         imageView.frame = CGRectMake(ix, iy, iw, ih)
  77.        
  78.         centerPoint = imageView.center
  79.        
  80.         imageContainer.addSubview(imageView)
  81.         self.view.addSubview(imageContainer)
  82.         self.view.addSubview(topImageView)
  83.         self.view.addSubview(fillerView1)
  84.         self.view.addSubview(fillerView2)
  85.        
  86.         print("Loaded image size: \(image.size)")
  87.     }
  88.  
  89.     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  90.         let boundsScale = imageView.bounds.size.width / imageView.bounds.size.height
  91.         let imageScale = image!.size.width / image!.size.height
  92.        
  93.         let size = (image?.size)!
  94.        
  95.         var canvasSize = size
  96.        
  97.         if boundsScale > imageScale {
  98.             canvasSize.width =  canvasSize.height * boundsScale
  99.         }
  100.         else{
  101.             canvasSize.height =  canvasSize.width / boundsScale
  102.         }
  103.        
  104.         let xScale = canvasSize.width / imageView.bounds.width
  105.         let yScale = canvasSize.height / imageView.bounds.height
  106.        
  107.         let center = CGPointApplyAffineTransform(imageView.center, CGAffineTransformScale(CGAffineTransformIdentity, xScale, yScale))
  108.        
  109.         let xCenter = center.x
  110.         let yCenter = center.y
  111.        
  112.         UIGraphicsBeginImageContextWithOptions(canvasSize, false, 0);
  113.         let context = UIGraphicsGetCurrentContext()!
  114.        
  115.         //Apply transformation
  116.         CGContextTranslateCTM(context, xCenter, yCenter)
  117.        
  118.         CGContextConcatCTM(context, imageView.transform)
  119.        
  120.         CGContextTranslateCTM(context, -xCenter, -yCenter)
  121.        
  122.         var drawingRect : CGRect = CGRectZero
  123.         drawingRect.size = canvasSize
  124.        
  125.         //Transaltion
  126.         drawingRect.origin.x = (xCenter - size.width*0.5)
  127.         drawingRect.origin.y = (yCenter - size.height*0.5)
  128.        
  129.         //Aspectfit calculation
  130.         if boundsScale > imageScale {
  131.             drawingRect.size.width =  drawingRect.size.height * imageScale
  132.         }else{
  133.             drawingRect.size.height = drawingRect.size.width / imageScale
  134.         }
  135.        
  136.         image!.drawInRect(drawingRect)
  137.        
  138.         let preview = UIGraphicsGetImageFromCurrentImageContext()
  139.         UIGraphicsEndImageContext()
  140.         let previewVC = segue.destinationViewController as! PreviewViewController
  141.         previewVC.preview = preview        
  142.  
  143. //OLD METHOD
  144.        
  145. //        UIGraphicsBeginImageContextWithOptions(imageContainer.bounds.size, false, 0)
  146. //        self.imageContainer.drawViewHierarchyInRect(imageContainer.bounds, afterScreenUpdates: true)
  147. //        let screenshot = UIGraphicsGetImageFromCurrentImageContext()!
  148. //        UIGraphicsEndImageContext()
  149.        
  150. //        let cropRect = CGRectMake(tx, ty, tw, th)
  151. //        let preview = getSubImageFrom(screenshot, rect: cropRect)
  152. //        let previewVC = segue.destinationViewController as! PreviewViewController
  153. //        previewVC.preview = preview
  154.     }
  155.    
  156.     func getSubImageFrom(img:UIImage!, rect:CGRect) -> UIImage! {
  157.         UIGraphicsBeginImageContext(rect.size)
  158.         let context = UIGraphicsGetCurrentContext()
  159.         let drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, img.size.width, img.size.height)
  160.         CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height))
  161.         img.drawInRect(drawRect)
  162.         let subImage = UIGraphicsGetImageFromCurrentImageContext()
  163.         UIGraphicsEndImageContext()
  164.         return subImage
  165.     }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement