创建检查按钮将产生错误(swift3)

时间:2017-06-11 04:28:18

标签: ios swift swift3 null

我的代码从letImageVIEW获取图像并尝试将图像置于其上,然后将其称为imageviewB。问题是,如果letImageVIEW上没有图像,应用程序崩溃了。

如果letImageVIEW上没有图片,如何实施检查以防止我的应用崩溃?

  @IBAction func add(_ sender: Any) {
     let left:UIImage = letImageVIEW.image!
     left.draw(in: CGRect(x: newSize2.width/10.0,y: newSize2.height/8.9,width: newSize2.width/2.5,height:   newSize2.height/1.29), blendMode:CGBlendMode.normal, alpha:1.0)
 }

2 个答案:

答案 0 :(得分:0)

试试这个:

    if let left:UIImage = letImageVIEW.image {
          left.draw(in: CGRect(x: newSize2.width/10.0,y: newSize2.height/8.9,width: newSize2.width/2.5,height:   newSize2.height/1.29), blendMode:CGBlendMode.normal, alpha:1.0)
    }else {
         print("No Image...")
   }

答案 1 :(得分:0)

您可以使用<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:id="@+id/parentLayout" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <ListView app:layout_behavior="@string/appbar_scrolling_view_behavior" android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="match_parent" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/stat_sys_download" /> </android.support.design.widget.CoordinatorLayout> 打开选项图片的解包     请在下面找到示例。

guard