Swift3 如何在imageview swift 3中设置图像?
因此,我有一个名为cashOrCredit的图像视图,我试图通过编程设置它的图像,但不知何故无法设置 首先,我将图像设置为这样Swift3 如何在imageview swift 3中设置图像?,swift3,uiimageview,Swift3,Uiimageview,因此,我有一个名为cashOrCredit的图像视图,我试图通过编程设置它的图像,但不知何故无法设置 首先,我将图像设置为这样 cell.cashOrCredit.image = UIImage(named: "cash1.png") 我得到一个错误,说需要一个“,”分隔符 然后我就这样试过了 var cashImage: UIImage? cashImage = "cash1.png" cell.cashOrCredit.image = cashIma
cell.cashOrCredit.image = UIImage(named: "cash1.png")
我得到一个错误,说需要一个“,”分隔符
然后我就这样试过了
var cashImage: UIImage?
cashImage = "cash1.png"
cell.cashOrCredit.image = cashImage
但我得到一个线程1 EXC错误指令。
我似乎不明白出了什么问题
这里是错误
检查现金或信用证是否为uiimageview 第二, cashImage=UIImage(名为:“cash1.png”) 如果这不起作用,试试看 手机?。现金或信用卡图像 检查单元格是否为零 在Swift代码的上下文中,EXC_BAD_指令通常意味着您遇到了编译器陷阱,也就是说,由于运行时检测到错误,编译器将未定义的指令插入到代码中。最常见的原因是:
cell.cashOrCredit.image = UIImage(named: "cash1")
并检查Assets.xcsets中是否有“cash1.png”图像
如果你得到了答案,就放弃投票给我的答案。删除“.png”:
您还可以通过编程方式进行设置:
let cellImage = UIImageView(frame: CGRect(x: X, y: Y, width: WIDTH, height: HEIGHT))
cellImage.image = UIImage(named: "cash1")
cell.addSubview(cellImage)
为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.
}
注意:项目的Assets.xcsets中必须存在“生日标志”类型的图像。
我附上了截图,如果你需要任何帮助,请参考它
****//在要将图像添加到UIImageView的任何位置使用。[这里我使用了一个函数&在该函数中,我编写了一个代码,将image设置为UIImageView]****
享受。。! 拍摄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)
}
调用选择picker方法,无论您想在哪里调用。@IBOutlet弱var cashOrCredit:UIImageView!这就是现金信用卡的运作方式defined@A.G您是否将图像作为图像集放在“资源”文件夹中??或者直接拖动到你的项目??即使你拼错了图像名称,它也不会使你的程序崩溃。我认为错误在别的地方。你能发布调试错误吗?我尝试了两种方法,但它仍然给我相同的结果error@A.G你能告诉我调试中的错误吗??。Pic或其他??如果
现金或信用卡
是IBOutlet,您应该在Interface Builder中验证它是否正确连接。
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)
}