Прямо внизу у меня 2 кнопки. кнопку места необходимо нажать перед кнопкой magicjohnson, иначе произойдет ошибка выполнения.

    @IBAction func magicJohnson(_ sender: Any) {
        let photo = self.imageV.image
        let data = UIImagePNGRepresentation(photo!)
        if cdHandler.saveObject(pic:  data!){
        }}

    @IBAction func place(_ sender: Any) {
        let image = UIImage.imageWithView(view: theView)
        let heart:UIImage = UIImage(named: "words")!
        let left:UIImage = image
        let newSize2 = CGSize(width: left.size.width, height: left.size.height)
        UIGraphicsBeginImageContextWithOptions(newSize2, false, left.scale)
        left.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
        heart.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        imageV.image = newImage
    }

Как я могу заставить код, по сути, сделать это /

 @IBAction func magicJohnson(_ sender: Any) {
     FIRST {
   let image = UIImage.imageWithView(view: theView)
        let heart:UIImage = UIImage(named: "words")!
        let left:UIImage = image
        let newSize2 = CGSize(width: left.size.width, height: left.size.height)
        UIGraphicsBeginImageContextWithOptions(newSize2, false, left.scale)
        left.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
        heart.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        imageV.image = newImage
            }
second{
        let photo = self.imageV.image
        let data = UIImagePNGRepresentation(photo!)
        if cdHandler.saveObject(pic:  data!){
        }
        }}
0
user8105388 14 Фев 2018 в 00:03

1 ответ

Лучший ответ

Попробуйте это, объявите placeBu как IBOutlet

@IBAction func magicJohnson(_ sender: Any) {
    self.place(self.placeBu) // may be self.place(UIButton()) if you don't care of sender instead of the outlet
    let photo = self.imageV.image
    let data = UIImagePNGRepresentation(photo!)
    if cdHandler.saveObject(pic:  data!){
    }}

Или же

@IBAction func magicJohnson(_ sender: Any) {

    if let photo = self.imageV.image
    { 
        let data = UIImagePNGRepresentation(photo!)
           if cdHandler.saveObject(pic:  data!){
        }}

    }
  }

ИЛИ в построителе интерфейса для кнопки magicJohnson установите значение false

@IBAction func place(_ sender: Any) {
    let image = UIImage.imageWithView(view: theView)
    let heart:UIImage = UIImage(named: "words")!
    let left:UIImage = image
    let newSize2 = CGSize(width: left.size.width, height: left.size.height)
    UIGraphicsBeginImageContextWithOptions(newSize2, false, left.scale)
    left.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
    heart.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    imageV.image = newImage
    self.magicJohnson.isEnabled = true
}
1
Sh_Khan 14 Фев 2018 в 00:21