如何在imageview swift 3中设置图像?

时间:2017-08-22 08:23:57

标签: swift3 uiimageview

所以我有一个名为cashOrCredit的图像视图,我试图以编程方式设置它的图像,但不知何故不能。

首先我像这样设置图像

cell.cashOrCredit.image = UIImage(named: "cash1.png")

我得到一个错误,说需要一个“,”分隔符。

然后我就这样试试了

var cashImage: UIImage?
cashImage = "cash1.png"
cell.cashOrCredit.image = cashImage

但是我得到了一个THREAD 1 EXC BAD INSTRUCTION错误。 我似乎无法理解出了什么问题?

这是错误

enter image description here

5 个答案:

答案 0 :(得分:7)

试试这个:

cell.cashOrCredit.image = UIImage(named: "cash1")

并检查" cash1.png"图像在Assets.xcassets中是否可用。

如果你得到解决方案,那就放弃我的回答。

答案 1 :(得分:7)

  

针对Swift 3进行了更新:

使用下面的简单代码,将图像设置为UIImageView;

class YourViewControllerName: UIViewController {

        var mYourImageViewOutlet: UIImageView?

        func addImageToUIImageView{
            var yourImage: UIImage = UIImage(named: "Birthday_logo")!
            mYourImageViewOutlet.image = yourImage
        } // call this function where you want to set image.
     }

注意:" Birthday_logo"您的项目的Assets.xcassets中必须存在图像类型。 如果您需要任何帮助,请附上截图。请参阅。

**** //用于任何想要将图像添加到UIImageView的地方。 [这里我使用了一个功能&在该函数中,我编写了一个代码来将图像设置为UIImageView] ****

享受..!enter image description here

答案 2 :(得分:1)

获取ImageView的出口

@IBOutlet weak var imgProfile: UIImageView!

仔细阅读以下代码,其中包含的内容将有助于您从相机中拾取图像或捕获图像。

func choosePicker() {
        let alertController = UIAlertController(title: "Select Option", message: nil, preferredStyle: (IS_IPAD ? UIAlertControllerStyle.alert : UIAlertControllerStyle.actionSheet))
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { action -> Void in
        })
        let gallery = UIAlertAction(title: "From Gallery", style: UIAlertActionStyle.default
            , handler: { action -> Void in
                self.openPicker(isCamera: false)
        })
        let camera = UIAlertAction(title: "Take Photo", style: UIAlertActionStyle.default
            , handler: { action -> Void in
                self.openPicker(isCamera: true)
        })
        alertController.addAction(gallery)
        alertController.addAction(camera)
        alertController.addAction(cancelAction)
        self.present(alertController, animated: true, completion: nil)
    }

func openPicker(isCamera : Bool) {
        let picker:UIImagePickerController?=UIImagePickerController()
        if isCamera {
            picker!.sourceType = UIImagePickerControllerSourceType.camera
        } else {
            picker!.sourceType = UIImagePickerControllerSourceType.photoLibrary
            picker!.allowsEditing = true
        }
        picker?.delegate = self

        if UIDevice.current.userInterfaceIdiom == .phone {
            self.present(picker!, animated: true, completion: nil)
        }
        else {
            picker!.modalPresentationStyle = .popover
            present(picker!, animated: true, completion: nil)//4
            picker!.popoverPresentationController?.sourceView = imgProfile
            picker!.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
        }
    }

// MARK: - UIImagePickerControllerDelegate Methods
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            self.imgProfile.image = image
        }
        picker.dismiss(animated: true, completion: nil);
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }

要在任何地方调用 choosePicker 方法。

答案 3 :(得分:0)

检查cashOrCredit是否为uiimageview。

其次,

cashImage = UIImage(名称:" cash1.png")

如果这不起作用,请尝试

细胞? cashOrCredit.image

检查细胞是否为零?

在Swift代码的上下文中,EXC_BAD_INSTRUCTION通常意味着您遇到了编译器陷阱,即由于在运行时检测到错误而被编译器插入到代码中的未定义指令。其中最常见的原因是:

  1. 无法打开可选项 - 这可以是强制解包(!)或隐式解包(访问隐式解包的可选项,即nil)。

  2. 数组越界

  3. 失败的强制转换(作为!),因为该值为nil可选或因为该值类型错误

答案 4 :(得分:0)

删除" .png":

cell.cashOrCredit.image = UIImage(named: "cash1")

您也可以通过编程方式设置它:

let cellImage = UIImageView(frame: CGRect(x: X, y: Y, width: WIDTH, height: HEIGHT))
cellImage.image = UIImage(named: "cash1")
cell.addSubview(cellImage)