2016-09-15 95 views
0

我有以下在該應用功能:裁剪具有相同的CGRect相同的UIImage給出不同的結果

  1. 用戶採取(或選擇)的圖像(以下originalImage)。
  2. originalImage被髮送到一些外部API,它返回我需要添加到originalImage的點的座標數組。
  3. 由於點總是位於一個區域(面部),因此我想在靠近面部邊框處裁剪originalImage並僅向用戶顯示裁剪的結果。
  4. 裁剪結果顯示後,我逐個添加點。

這裏是一個沒有工作的代碼(除發送圖像,讓我們說這已經發生)

class ScanResultViewController{ 

    @IBOutlet weak var scanPreviewImageView: UIImageView! 

    let originalImage = ORIGINAL_IMAGE //meaning we already have it 

    let scanDots = [["x":123, "y":123], ["x":234, "y":234]]//total 68 coordinates 

    var cropRect:CGRect! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.setScanImage() 
    } 

    override func viewDidAppear(animated: Bool) { 
     super.viewDidAppear(animated) 

     self.animateScan(0) 
    } 


    func setScanImage(){ 

     self.cropRect = self.getCropRect(self.scanDots, sourceImage:self.originalImage) 

     let croppedImage = self.originalImage.imageAtRect(self.cropRect) 

     self.scanPreviewImageView.image = croppedImage 
     self.scanPreviewImageView.contentMode = .ScaleAspectFill 

    } 


    func animateScan(index:Int){ 

     let i = index 

     self.originalImage = self.addOnePointToImage(self.originalImage, pointImage: GREEN_DOT!, point: self.scanDots[i]) 

     let croppedImage = self.originalImage.imageAtRect(self.cropRect) 

     self.scanPreviewImageView.image = croppedImage 
     self.scanPreviewImageView.contentMode = .ScaleAspectFill 

     if i < self.scanDots.count-1{ 

      let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))) 
      dispatch_after(delay, dispatch_get_main_queue()) { 

       self.animateScan(i+1) 
      } 
     } 
    } 


    func addOnePointToImage(sourceImage:UIImage, pointImage:UIImage, point: Dictionary<String,CGFloat>)->UIImage{ 

     let rect = CGRect(x: 0, y: 0, width: sourceImage.size.width, height: sourceImage.size.height) 

     UIGraphicsBeginImageContextWithOptions(sourceImage.size, true, 0) 
     let context = UIGraphicsGetCurrentContext() 

     CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor) 
     CGContextFillRect(context, rect) 

     sourceImage.drawInRect(rect, blendMode: .Normal, alpha: 1) 

     let pointWidth = sourceImage.size.width/66.7 

     pointImage.drawInRect(CGRectMake(point["x"]!-pointWidth/2, point["y"]!-pointWidth/2, pointWidth, pointWidth), blendMode: .Normal, alpha: 1) 

     let result = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return result 
    } 


    func getCropRect(points: Array<Dictionary<String,CGFloat>>, sourceImage:UIImage)->CGRect{ 

     var topLeft:CGPoint = CGPoint(x: points[0]["x"]!, y: points[0]["y"]!) 
     var topRight:CGPoint = CGPoint(x: points[0]["x"]!, y: points[0]["y"]!) 
     var bottomLeft:CGPoint = CGPoint(x: points[0]["x"]!, y: points[0]["y"]!) 
     var bottomRight:CGPoint = CGPoint(x: points[0]["x"]!, y: points[0]["y"]!) 

     for p in points{ 

      if p["x"]<topLeft.x {topLeft.x = p["x"]!} 
      if p["y"]<topLeft.y {topLeft.y = p["y"]!} 

      if p["x"]>topRight.x {topRight.x = p["x"]!} 
      if p["y"]<topRight.y {topRight.y = p["y"]!} 

      if p["x"]<bottomLeft.x {bottomLeft.x = p["x"]!} 
      if p["y"]>bottomLeft.y {bottomLeft.y = p["y"]!} 

      if p["x"]>bottomRight.x {bottomRight.x = p["x"]!} 
      if p["y"]>bottomRight.y {bottomRight.y = p["y"]!} 

     } 

     let rect = CGRect(x: topLeft.x, y: topLeft.y, width: (topRight.x-topLeft.x), height: (bottomLeft.y-topLeft.y)) 

     return rect 
    } 

} 

extension UIImage{ 

    public func imageAtRect(rect: CGRect) -> UIImage { 
     let imageRef: CGImageRef = CGImageCreateWithImageInRect(self.CGImage, rect)! 
     let subImage: UIImage = UIImage(CGImage: imageRef) 

     return subImage 
    } 

} 

的問題是setScanImage所需區域精確裁剪並顯示,但是當animateScan方法被調用時,同一圖像的不同區域被裁剪(並顯示),雖然cropRect是相同的,並且originalImage的大小完全相同。

任何想法,傢伙?

順便說一句,如果我顯示originalImage沒有裁剪它一切順利。

回答

0

所以最後經過約10小時的時間淨額( 和大量的計算器社區 幫助:-)我設法解決這個問題:您需要更改以下

在功能addOnePointToImage

在這一行:

UIGraphicsBeginImageContextWithOptions(sourceImage.size, true, 0) 

你需要改變的最後一個參數(代表比例)爲1:

UIGraphicsBeginImageContextWithOptions(sourceImage.size, true, 1) 

這完全解決了這個問題。

相關問題