3

I use AVFoundation framework to capture and display the camera video preview and it works. Now, I want add my own UIImageView on the top of camera preview, here are some fragments of my code.

            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            previewLayer!.videoGravity = AVLayerVideoGravityResizeAspect
            previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
            previewLayer!.frame = previewView.bounds
            previewView.layer.addSublayer(previewLayer!)

            var imageView : UIImageView
            imageView = UIImageView(frame: CGRect(x: 130,y: 320,width: 60,height: 30))
            imageView.image  = UIImage(named:"scanbutton.png")                
            previewView.addSubview(imageView)

            self.captureSession!.startRunning()

The "scanbutton.png" image file is in the project folder.

The UIImageView cannot be displayed. Can anyone tell what's wrong, thanks.

Sean Lintern
  • 3,103
  • 22
  • 28
Jun Fang
  • 351
  • 1
  • 4
  • 21
  • To clarify, do you see the image on your screen? but not in the recording? – Sean Lintern Apr 25 '16 at 10:47
  • I cannot see the image on the screen. – Jun Fang Apr 25 '16 at 12:07
  • try changing previewView.layer.addSublayer(previewLayer!) to previewView.insertSublayer(previewLayer, atIndex: 0) – Sean Lintern Apr 25 '16 at 14:23
  • still cannot display. I wonder if the UIImageView is empty. According to http://stackoverflow.com/questions/13272512/add-uiimage-in-calayer, I only add sublayer of my own image like this: 'let myImage = UIImage(named: "scanbutton.png")?.CGImage; let myLayer = CALayer() myLayer.frame = CGRect(x: 130,y: 320,width: 60,height: 30) myLayer.contents = myImage previewView.layer.addSublayer(myLayer)' It doesn't display anything. – Jun Fang Apr 25 '16 at 14:56

0 Answers0