2016-09-15 96 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) 

这完全解决了这个问题。

相关问题