-1

I am trying to make an image with graphics context to then share it on instagram. I have the code bellow from this q: How do I draw on an image in Swift?. The issue is that for some reason the second image I add is not showing up. Neither is the label I am trying to use.

How can I make this work correctly

            let image = self.getImage(url: (Auth.auth().currentUser?.photoURL)!)
            let imageView = UIImageView(frame: self.view.frame)
            imageView.contentMode = .scaleAspectFit
            imageView.image = UIImage(named: "backgroundImageToShare")
            imageView.drawOnCurrentImage(anotherImage: image, mode: .addSubview) { parentSize, newImageSize in
                //(parentSize.width/2)-(newImageSize.width/2)
                //(parentSize.height/2)-(newImageSize.height/2)
                return CGRect(x: 50, y: 50, width: 118, height: 118)
            }

            let library = ALAssetsLibrary()
            let finalImage = imageView.image//self.getImage(url: (Auth.auth().currentUser?.photoURL)!)
            library.writeImage(toSavedPhotosAlbum: finalImage?.cgImage, metadata: nil) { (url, error) in
Extensions:
extension UIImage {

    typealias RectCalculationClosure = (_ parentSize: CGSize, _ newImageSize: CGSize)->(CGRect)

    func with(image named: String, rectCalculation: RectCalculationClosure) -> UIImage {
        return with(image: UIImage(named: named), rectCalculation: rectCalculation)
    }

    func with(image: UIImage?, rectCalculation: RectCalculationClosure) -> UIImage {

        if let image = image {
            UIGraphicsBeginImageContext(size)

            draw(in: CGRect(origin: .zero, size: size))
            image.draw(in: rectCalculation(size, image.size))

            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage!
        }
        return self
    }
}
extension UIImageView {

    enum ImageAddingMode {
        case changeOriginalImage
        case addSubview
    }

    func drawOnCurrentImage(anotherImage: UIImage?, mode: ImageAddingMode, rectCalculation: UIImage.RectCalculationClosure) {
        guard let image = image else {
            return
        }

        switch mode {
        case .changeOriginalImage:
            self.image = image.with(image: anotherImage, rectCalculation: rectCalculation)

        case .addSubview:
            let newImageView = UIImageView(frame: rectCalculation(frame.size, image.size))
            newImageView.image = anotherImage
            newImageView.layer.masksToBounds = true
            newImageView.layer.cornerRadius = newImageView.frame.height/2
            newImageView.layer.borderWidth = 2
            newImageView.layer.borderColor = UIColor.white.cgColor

            newImageView.layer.shadowColor = UIColor.white.cgColor
            newImageView.layer.shadowRadius = 6
            newImageView.layer.shadowOpacity = 0.2
            newImageView.layer.shadowOffset = CGSize(width: 0, height: 0)
            self.superview?.addSubview(newImageView)
            let label = UILabel(frame: CGRect(x: 50
                , y: 50
                , width: 100
                , height: 30))
            label.text = " Tests stsg"
            label.textColor = .black
            self.superview?.addSubview(label)
        }
    }
}

1 Answers1

0

I am trying to make an image with graphics context

No, you are not. By saying mode: .addSubview that is exactly what you are saying not to do.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Then how do I make the image larger and center it, and also ad a uilabel? –  Sep 20 '19 at 02:29
  • That is a very broad question. It is very basic drawing but I'm not going to write the code for you in a comment. Your question what why does _this code_ not draw your second image and label, and I answered that: it's because you said to overlay another view on the image view and _not_ draw a new image in a graphics context. – matt Sep 20 '19 at 02:31