2017-04-01 54 views
1

我遵循下面的代码,它根本不显示图像。这里会出现什么错误?我试图调试newimage显示nil。如何在swift 3.0中编写图像上的菱形和菱形?

我无法解决it.so任何帮助,将不胜感激

func textOnImage(text: NSString, atPoint: CGPoint,capturedImage:UIImage?) -> UIImage?{ 

     if let capturedImage = capturedImage{ 

      // Setup the font specific variables 
      let textColor = UIColor.white 
      let textFont = UIFont(name: "Helvetica", size: 14)! 

      // Setup the image context using the passed image 


      let scale = UIScreen.main.scale 

      UIGraphicsBeginImageContextWithOptions(capturedImage.size, false, scale) 



      // Setup the font attributes that will be later used to dictate how the text should be drawn 
      let textFontAttributes = [ 
       NSFontAttributeName: textFont, 
       NSForegroundColorAttributeName: textColor, 
       ] as [String : Any] 

      // Create a point within the space that is as bit as the image 


      let rect = CGRect(x: atPoint.x, y: atPoint.y, width: capturedImage.size.width, height: capturedImage.size.height) 

      // Draw the text into an image 

      text.draw(in: rect, withAttributes: textFontAttributes) 



      // Create a new image out of the images we have created 
      let newImage = UIGraphicsGetImageFromCurrentImageContext() 

      // End the context now that we have the image we need 
      UIGraphicsEndImageContext() 
//Pass the image back up to the caller 
      return newImage! 

     } 
     return nil 

    } 
+0

看起来就像图像帧丢失。请检查 – adarshaU

+0

你能否提一下它在哪里? – Mickel

回答

0

包括以下线

capturedImage.draw(in: CGRect(x: 0, y: 0, width: capturedImage.size.width, height:capturedImage.size.height)) 

所以你的方法看起来像

func textOnImage(text: NSString, atPoint: CGPoint,capturedImage:UIImage?) -> UIImage?{ 

     if let capturedImage = capturedImage{ 

      // Setup the font specific variables 
      let textColor = UIColor.white 
      let textFont = UIFont(name: "Helvetica", size: 14)! 

      // Setup the image context using the passed image 


      let scale = UIScreen.main.scale 

      UIGraphicsBeginImageContextWithOptions(capturedImage.size, false, scale) 



      // Setup the font attributes that will be later used to dictate how the text should be drawn 
      let textFontAttributes = [ 
       NSFontAttributeName: textFont, 
       NSForegroundColorAttributeName: textColor, 
       ] as [String : Any] 



      capturedImage.draw(in: CGRect(x: 0, y: 0, width: capturedImage.size.width, height:capturedImage.size.height)) 



      // Create a point within the space that is as bit as the image 


      let rect = CGRect(x: atPoint.x, y: atPoint.y, width: capturedImage.size.width, height: capturedImage.size.height) 

      // Draw the text into an image 

      text.draw(in: rect, withAttributes: textFontAttributes) 



      // Create a new image out of the images we have created 
      let newImage = UIGraphicsGetImageFromCurrentImageContext() 

      // End the context now that we have the image we need 
      UIGraphicsEndImageContext() 

      //Pass the image back up to the caller 
      return newImage! 

     } 
     return nil 

    } 

} 
+0

非常感谢。有效 – Mickel