2017-03-09 65 views
-1

在我發佈一些代碼並說出一些事情之前,我正在使用此窗格拍攝照片和視頻:https://github.com/imaginary-cloud/CameraManager 我想要做的僅僅是拍攝圖像,在底部放置一個文本(該文本可以通過我已經創建的現有短語陣列進行拾取),並且我想將這兩個實體合併爲一個新的圖像,它將顯示在新的視圖控制器中並保存在相機膠捲中,當然是原來的。我怎樣才能做到這一點?由於如何在Swift3中捕獲的圖像上添加UIImage

這裏是我的capturePhoto代碼:

let when = DispatchTime.now() + 5 
    func capturePicture(){ 
     _ = cameraManager.addPreviewLayerToView(cameraPreview, newCameraOutputMode: CameraOutputMode.stillImage) 

     DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) { 
      // do stuff x seconds later 

      self.cameraManager.capturePictureWithCompletion({ (image, error) -> Void in 

       self.cameraPreview.image = image 

      }) 

     } 


    } 
+0

使用UIImage.draw –

回答

0

嘗試以下

UIGraphicsBeginImageContextWithOptions(image!.size, false, 0.0) 
// I tought cameraPreview is an imageview 
cameraPreview.layer.render(in: UIGraphicsGetCurrentContext()!) 
YOUR_UILABEL.layer.render(in: UIGraphicsGetCurrentContext()!) 
let newImage = UIGraphicsGetImageFromCurrentImageContext() 
let imageData = UIImageJPEGRepresentation(newImage!, 0.9) 
let pngImage = UIImage(data: imageData!) 
UIGraphicsEndImageContext() 

的代碼片段然後你的新圖像目標是準備在pngImage

和協議樣品低於

// you can add this protocol to first viewcontroller 
protocol ReceiveImageProtocol: class { 

    func onReceiveImage(image: UIImage) 

} 

class FirstViewController: UIViewController, ReceiveImageProtocol { 

    ... 

    internal func onReceiveImage(image: UIImage) { 
     // use merger function for image and label 
    } 

    internal func openSeconViewController() { 
     let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController 
     secondViewController.imageProtocol = self 
     // push or present secondViewController 
    } 
} 

class SecondViewController: UIViewController { 

    public weak var imageProtocol: ReceiveImageProtocol! 

    // when you capture return back to firstviewcontroller 
    // using pop or dismiss 
    // and don't forget to call 
    // self.imageProtocol.onReceiveImage(image: YOUR_CAPTURED_IMAGE) 
} 
+0

謝謝,將嘗試實施它,我會報告回來,如果它的工作/沒有工作。現在感謝! –

+0

我的朋友,我有一個問題,我沒有在原始問題中編寫這個細節,但我的隨機標籤是在另一個視圖控制器中,我怎麼能在我的主人那裏調用它,上面的函數位於哪個? –

+0

如果您只捕獲一個圖像,則可以通過委託向先前的視圖控制器或多個圖像發送此圖像,當您推送或呈現捕獲圖像視圖控制器時,從第一個視圖控制器發送標籤以捕獲圖像視圖控制器。 – abdullahselek

0

您可以使用UIGraphicsBeginImageContextWithOptions來完成此操作。

UIGraphicsBeginImageContextWithOptions(self.cameraPreview.bounds.size, false, UIScreen.main.scale) self.imageView.layer.render(in: UIGraphicsGetCurrentContext()!) 
let captureImage = UIGraphicsGetImageFromCurrentImageContext()! 
UIGraphicsEndImageContext() 

然後您將捕獲的圖像與其上的文字。

+0

也會試試這個!謝謝! –