Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将图像添加到UIImageView?_Ios_Swift_Uiimageview_Core Graphics - Fatal编程技术网

Ios 如何将图像添加到UIImageView?

Ios 如何将图像添加到UIImageView?,ios,swift,uiimageview,core-graphics,Ios,Swift,Uiimageview,Core Graphics,我正试图将图像添加到UIImageView,但在添加之后UIImageView为零。这是我的密码: class ViewController: UIViewController { @IBOutlet var myView: UIView! @IBOutlet weak var testImg: UIImageView! override func viewDidLoad() { super.viewDidLoad() UIGra

我正试图将图像添加到
UIImageView
,但在添加之后
UIImageView
为零。这是我的密码:

    class ViewController: UIViewController {

    @IBOutlet var myView: UIView!
    @IBOutlet weak var testImg: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        UIGraphicsBeginImageContext(CGSize(width: 500,height: 500))
                var path = UIBezierPath()
        path.lineWidth = 1.0
        path.moveToPoint(CGPoint(x: 150, y: 150))
        var x = 151
        for ;x<300;x++
        {
            path.addLineToPoint(CGPoint(x: x, y: x))
            path.moveToPoint(CGPoint(x: x, y: x))
        }
        path.stroke()
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
                testImg = UIImageView(image: img)
        testImg.frame = CGRect(x: 0,y: 0,width: 500,height: 500)
        myView.addSubview(testImg)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
这一行代码覆盖了
UIImageView
IBOutlet
,删除它并简单地执行操作
testImg.image=img

修复问题。

简单方法:将该图像拖放到Xcode上的文件视图/项目导航器

选择图像视图-然后选择属性inspect-在图像下-选择要加载的图像的文件名

尽可能享受拖拉和下降

硬道:

if let filePath = Bundle.main.path(forResource: "imageName", ofType:     "jpg"), let image = UIImage(contentsOfFile: filePath) {    imageView.contentMode = .scaleAspectFit
    imageView.image = image
}

错误应该与推送或显示
ViewController
的方式有关。当您展示或演示时,似乎尚未创建插座。应使用以下示例中的
instantialeviewcontrollerwhiteIdentifier

let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("YourStoryBoardId")
self.navigationController?.pushViewController(viewController!, animated: true)
简单地说:

if let image = img {
    // you don't have to instantiate an imageView again, IBOutlet handles it for you    
    testImg.image = image
    print("Works")
    // if you don't see this output in your debug console, the condition never succeeds and an img is really nil
}

你连接插座了吗?是的,两个插座都是连接的。情节提要ViewController类是在identity inspector中设置的吗?是的,它是设置的。我没有文件,我正在通过UIBezierPath创建图像。你的意思是我无法通过代码创建图像并将其添加到UIImageView?抱歉,我想说的是在这方面无能为力。是不是一样的
testImg=UIImageView(image:img)
添加了if块,设置图像属性时出现了相同的错误by
testImg=UIImageView(image:img)
您正在用新的UIImageView从头开始重写您原来的IBOutlet。我的回答对您有帮助吗?:)是的,非常感谢)我很久以前就把它标记为答案)我只有一个视图,在
super.viewDidLoad()之后添加了这两个字符串,没有任何更改。我认为这是UIImageView的问题
if let image = img {
    // you don't have to instantiate an imageView again, IBOutlet handles it for you    
    testImg.image = image
    print("Works")
    // if you don't see this output in your debug console, the condition never succeeds and an img is really nil
}