Swift-使用未解析的标识符'self'/使用未解析的标识符'Meme'/使用未解析的标识符'memedImage'

时间:2018-10-21 14:05:49

标签: ios swift xcode

我收到错误消息使用未解决的标识符“自我”以及错误消息使用未解决的标识符“ Meme”;你是说“时间”吗?以及在Xcode中使用无法解析的标识符“ memedImage”。我正在尝试制作MemeMaker,但无法为模因的修改后的图像创建结构。这是我的代码:

import UIKit


class ViewController: UIViewController, 
UIImagePickerControllerDelegate,
UINavigationControllerDelegate {

    @IBOutlet weak var ImageView: UIImageView!
    @IBOutlet weak var cameraButton: UIBarButtonItem!
    @IBOutlet weak var topTextField: UITextField!
    @IBOutlet weak var bottomTextField: UITextField!

    let textFieldDelegate = TextFieldDelegate()

    let memeTextAttributes:[NSAttributedString.Key: Any] = [
        NSAttributedString.Key.strokeColor: UIColor.black,
        NSAttributedString.Key.foregroundColor: UIColor.white,
        NSAttributedString.Key.font: UIFont(name: "HelveticaNeue- 
       CondensedBlack", size: 40)!,
       NSAttributedString.Key.strokeWidth: -3.5]



    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)


        topTextField.textAlignment = .center
        bottomTextField.textAlignment = .center
        topTextField.text = "Top Text"
        bottomTextField.text = "Bottom Text"

        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            cameraButton.isEnabled = true
        } else {
            cameraButton.isEnabled = false
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        //
        UIImagePickerController().delegate = self
        //
        self.topTextField.delegate = textFieldDelegate
        self.bottomTextField.delegate = textFieldDelegate
        //
        topTextField.defaultTextAttributes = memeTextAttributes
        bottomTextField.defaultTextAttributes = memeTextAttributes
        //

    }

    //Button to allow user pick an image from an Album
    @IBAction func pickImageFromAlbum(_ sender: Any) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        present(imagePicker, animated: true, completion: nil)
    }

    //Allows user to use camera to take a photo
    @IBAction func takeImageFromCamera(_ sender: Any) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .camera
        present(imagePicker, animated: true, completion: nil)
    }

    @objc internal func imagePickerController(_ picker: 
UIImagePickerController, didFinishPickingMediaWithInfo info: [String: 
Any]) {
    if let pickedImage =

  info[convertFromUIImagePickerControllerInfoKey
(UIImagePickerController.InfoKey.originalImage)] as? UIImage {

        ImageView.contentMode = .scaleToFill
        ImageView.image = pickedImage
        dismiss(animated: true, completion: nil)

        }
    }
}

func generateMemedImage() -> UIImage {

    // Render view to an image
    UIGraphicsBeginImageContext(self.UIView.frame.size) 
    //Use of unresolved identifier 'self'
    UIView.drawHierarchy(in: self.UIView.frame, afterScreenUpdates: 
true) //Use of unresolved identifier 'self'
    let memedImage:UIImage = 
UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return memedImage
}

func save() {
    // Creates the meme
    let meme = Meme(topText: topTextField.text!, bottomText: 
bottomTextField.text!, originalImage: UIImageView.image!, memedImage: 
memedImage) //Use of unresolved identifier 'memedImage'
}

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertFromUIImagePickerControllerInfoKey(_ input: 
UIImagePickerController.InfoKey) -> String {
    return input.rawValue
}

3 个答案:

答案 0 :(得分:0)

您已在类外部声明了函数,并尝试说此标识符属于当前类,但该函数没有任何类 generateMemedImage()

答案 1 :(得分:0)

让我们只关注代码中的许多问题之一-self的问题。

这是代码的结构。这是您的代码;我在这里所做的全部内容都被剪切掉了,使结构清晰可见:

class ViewController: UIViewController, 
    UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        // ...
}

func generateMemedImage() -> UIImage {
    UIGraphicsBeginImageContext(self.UIView.frame.size)
    // ...
}

您的class声明开始-结束!然后是一个全新的功能func generateMemedImage。它不在类声明中。它只是坐在茫茫人海中。这本身并不违法;全局函数在Swift中是合法的。但是您不能在全局函数中说self

因此,解决 问题的方法是将函数移入类声明中:

class ViewController: UIViewController, 
    UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        // ...
        func generateMemedImage() -> UIImage {
            UIGraphicsBeginImageContext(self.UIView.frame.size)
            // ...
        }
}

答案 2 :(得分:0)

我想我可以帮忙,我先在页面顶部声明您的Meme类或结构:

struct Meme {
    let topText: String!
    let bottomText: String!
    let originalImage: UIImage!
    let memedImage: UIImage!
}   

然后您需要更改:

memedImage: memedImage

至:

memedImage: generatedMemedImage()

这是您的状态:

func save() {
    // Creates the meme
    let meme = Meme(topText: topTextField.text!, bottomText: 
bottomTextField.text!, originalImage: UIImageView.image!, memedImage: 
memedImage) //Use of unresolved identifier 'memedImage'
}

这是解决方案:

func save() {
    // Creates the meme
    let meme = Meme(topText: topTextField.text!, bottomText: 
bottomTextField.text!, originalImage: UIImageView.image!, memedImage: 
**generatedMemedImage()**)
}

我希望这会有所帮助。