gpt4 book ai didi

ios - UIImageView 未正确初始化并返回 nil

转载 作者:搜寻专家 更新时间:2023-11-01 06:41:10 26 4
gpt4 key购买 nike

在我的 CathyTaskLogMessageViewController 类中,我有这段代码:

import UIKit

class CathyTaskLogMessageViewController: UIViewController {

@IBOutlet var tapGestureRecognizer: UITapGestureRecognizer!
@IBOutlet weak var messageTextView: UITextView!
var defaultPictureButton: UIButton = UIButton(type: UIButtonType.Custom) as UIButton

override func viewDidLoad() {
super.viewDidLoad()

defaultPictureButton = UIButton(type: UIButtonType.Custom) as UIButton
defaultPictureButton.frame = CGRectMake(100, 100, 40, 40)
defaultPictureButton.addTarget(self, action: "defaultPictureButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
defaultPictureButton.setImage(UIImage(named: "defaultPicture"), forState: UIControlState.Normal)
defaultPictureButton.layer.cornerRadius = 20.0
defaultPictureButton.clipsToBounds = true
defaultPictureButton.exclusiveTouch = true
self.view.addSubview(defaultPictureButton)

}

func defaultPictureButtonTapped(sender: UIButton) {

let imageViewerViewController = ImageViewerViewController()
imageViewerViewController.image = self.defaultPictureButton.imageView!.image
imageViewerViewController.centerPictureFromPoint(self.defaultPictureButton.frame.origin, ofSize: self.defaultPictureButton.frame.size, withCornerRadius: self.defaultPictureButton.layer.cornerRadius)

}

}

在我的 ImageViewerViewController 类中,我有以下代码:

import UIKit

class ImageViewerViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var image: UIImage!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

func centerPictureFromPoint(point: CGPoint, ofSize size: CGSize, withCornerRadius radius: CGFloat) {

self.imageView = UIImageView(frame: CGRect(x: point.x, y: point.y, width: size.width, height: size.height))
self.imageView.layer.cornerRadius = radius //I am getting the error here
self.imageView.clipsToBounds = true
self.imageView.image = self.image

}

}

我得到的错误是:fatal error: unwrappedly found nil while unwrapping an Optional value。在我看来,我通过编写 self.imageView = UIImageView(frame: CGRect(x: point.x, y: point.y, width: size.width, height:尺寸.高度))。但是,它返回零。有人对此有解决方案吗?

最佳答案

如果您不想通过 xib 或 Storyboard启动 imageView。更改属性声明:

@IBOutlet weak var imageView: UIImageView!

var imageView: UIImageView!

weak 导致 imageView 立即释放。

关于ios - UIImageView 未正确初始化并返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35150514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com