2017-06-28 1 views
0

J'essaie de créer une application de dessin qui se trouve au-dessus d'un UIImagePickerController après la prise de vue. Les fonctions de délégué tactile sont appelées correctement mais ne laissent pas de traces sur la vue comme elles sont supposées le faire. Ce que j'ai jusqu'à présent:L'image dessinée n'apparaît pas sur l'incrustation de l'appareil photo Swift

func createImageOptionsView() { //creates the view above the picker controller overlay view 
    let imgOptsView = UIView() 
    let scale = CGAffineTransform(scaleX: 4.0/3.0, y: 4.0/3.0) 
    imgOptsView.tag = 1 
    imgOptsView.frame = view.frame 
    let imageView = UIImageView() 
    imageView.frame = imgOptsView.frame 
    imageView.translatesAutoresizingMaskIntoConstraints = true 
    imageView.transform = scale //to account for the removal of the black bar in the camera 
    let useButton = UIButton() 
    imageView.tag = 2 
    imageView.contentMode = .scaleAspectFit 
    imageView.image = currentImage 
    useButton.setImage(#imageLiteral(resourceName: "check circle"), for: .normal) 
    useButton.translatesAutoresizingMaskIntoConstraints = true 
    useButton.frame = CGRect(x: view.frame.width/2, y: view.frame.height/2, width: 100, height: 100) 
    let cancelButton = UIButton() 
    colorSlider.previewEnabled = true 
    colorSlider.translatesAutoresizingMaskIntoConstraints = true 
    imgOptsView.addSubview(imageView) 
    imgOptsView.addSubview(useButton) 
    imgOptsView.addSubview(colorSlider) 
    imgOptsView.isUserInteractionEnabled = true 
    useButton.addTarget(self, action: #selector(ViewController.usePicture(sender:)), for: .touchUpInside) 
    picker.cameraOverlayView!.addSubview(imgOptsView) 
} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    mouseSwiped = false //to check if the touch moved or was simply dotted 
    let touch: UITouch? = touches.first 
    lastPoint = touch?.location(in: view) //where to start image context from 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    mouseSwiped = true 
    let touch: UITouch? = touches.first 
    let currentPoint: CGPoint? = touch?.location(in: view) 
    UIGraphicsBeginImageContext(view.frame.size) //begin drawing 
    tempDrawImage.image?.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(view.frame.size.width), height: CGFloat(view.frame.size.height))) 
    UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: CGFloat(lastPoint.x), y: CGFloat(lastPoint.y))) 
    UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: CGFloat((currentPoint?.x)!), y: CGFloat((currentPoint?.y)!))) 
    UIGraphicsGetCurrentContext()!.setLineCap(.round) 
    UIGraphicsGetCurrentContext()?.setLineWidth(1.0) 
    UIGraphicsGetCurrentContext()?.setStrokeColor(c) 
    UIGraphicsGetCurrentContext()!.setBlendMode(.normal) 
    UIGraphicsGetCurrentContext()?.strokePath() //move from lastPoint to currentPoint with these options 
    tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    lastPoint = currentPoint 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if !mouseSwiped { 
     UIGraphicsBeginImageContext(view.frame.size) 
     tempDrawImage.image?.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(view.frame.size.width), height: CGFloat(view.frame.size.height))) 
     UIGraphicsGetCurrentContext()!.setLineCap(.round) 
     UIGraphicsGetCurrentContext()?.setLineWidth(1.0) 
     UIGraphicsGetCurrentContext()?.setStrokeColor(c) 
     UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: CGFloat(lastPoint.x), y: CGFloat(lastPoint.y))) 
     UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: CGFloat(lastPoint.x), y: CGFloat(lastPoint.y))) 
     UIGraphicsGetCurrentContext()?.strokePath() 
     UIGraphicsGetCurrentContext()!.flush() 
     tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
    } 

    UIGraphicsBeginImageContext(mainImage.frame.size) 
    mainImage.image?.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(view.frame.size.width), height: CGFloat(view.frame.size.height)), blendMode: .normal, alpha: 1.0) 
    tempDrawImage.setNeedsDisplay() 
    mainImage.image = UIGraphicsGetImageFromCurrentImageContext() //paste tempImage onto the main image and then start over 
    tempDrawImage.image = nil 
    UIGraphicsEndImageContext() 
} 

func changedColor(_ slider: ColorSlider) { 
    c = slider.color.cgColor 
} 

Répondre

0

Ceci est juste une supposition, mais vaut la peine d'être prise. J'ai eu un problème similaire mais dans un contexte différent ... Je travaillais avec AVPlayer quand c'est arrivé. Dans mon cas, c'est parce que zPosition n'était pas correctement défini, donc les superpositions que j'essayais de dessiner apparaissaient derrière la vidéo. En supposant que ce soit le cas, la solution consisterait à régler la zPosition = -1 qui se déplace plus loin en arrière comme ceci:

AVPlayerView.layer?.zPosition = -1 

Cela a déplacé la couche de base derrière la couche de recouvrement (des nombres négatifs se déplacent plus loin de l'utilisateur Espace 3D.)

Je verrais si la vue contrôlée par votre UIImagePickerController vous permet de définir sa zPosition de manière similaire à ce que permet AVPlayer. Si je me souviens bien, toute vue devrait être en mesure de définir son zPosition. Alternativement, vous pouvez essayer de déplacer votre couche de recouvrement à une valeur positive comme zPosition = 1. (Je ne suis pas certain, mais j'imagine que la position par défaut est 0.)

+0

J'ai trouvé 'view.layer.zPosition' . Je l'essayerai une fois que je disposerai d'un appareil physique disponible –

+0

N'a pas fonctionné malheureusement, mais merci –