1

我想在用户拍摄照片时UIImage相机屏幕中添加自定义。我还希望该图像位于该捕获图像的前景中,该图像位于添加图像的同一位置。
这是解释我想要什么的演示图像
为了解决这个问题,我尝试了

let picker = UIImagePickerController()
picker.sourceType = .Camera
picker.view.addSubView(imageView) 

但是使用此代码它只会添加图像而不是添加图像与捕获图像

任何帮助表示赞赏。

4

2 回答 2

1

您可以使用UIImagePickerController'cameraOverlayView属性imageView在相机屏幕顶部添加:

let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
picker.cameraOverlayView = imageView

presentViewController(picker, animated: true, completion: nil)

要将图像添加到捕获照片的前景,您可以为以下内容创建扩展名UIImage

extension UIImage {
  func imageWithOverlayImage(overlayImage: UIImage) -> UIImage {
    let imageRect = CGRectMake(0, 0, self.size.width, self.size.height)

    UIGraphicsBeginImageContext(self.size)

    self.drawInRect(imageRect)
    overlayImage.drawInRect(imageRect)

    let resultImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return resultImage
  }
}

然后你可以从图像选择器的委托方法中调用它:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
  let image = info[UIImagePickerControllerOriginalImage] as! UIImage
  finalImage = image.imageWithOverlayImage(overlayImage!)
  dismissViewControllerAnimated(true, completion: nil)
}
于 2016-06-18T08:33:38.863 回答
0

尝试放置Overlayview。将自定义图像添加到叠加视图。

func openCamera(){

        let pickercontroller : UIImagePickerController = UIImagePickerController()
        pickercontroller.delegate = self

        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){

            pickercontroller.sourceType = UIImagePickerControllerSourceType.Camera
        }
        else{
        }

        pickercontroller.editing = false
        pickercontroller.showsCameraControls = false
        let graphRect:CGRect = CGRectMake(0, 0,pickercontroller.view.frame.size.width-20 ,pickercontroller.view.frame.size.height)

        let overlayView : UIView = UIView(frame: graphRect)
        overlayView.backgroundColor = UIColor(colorLiteralRed: 0.0/255.0, green: 0/255.0, blue: 0.0/255.0, alpha: 0.5)
        overlayView.layer.opaque = false
        overlayView.opaque = false

        var maskLayer : CAShapeLayer = CAShapeLayer()
        overlayView.layer.mask = maskLayer
      }
于 2016-07-16T07:02:17.210 回答