UIImage崩溃问题

时间:2015-12-01 21:50:15

标签: ios image swift uiimage

我的图像崩溃问题。如果我从VC2输入图像并点击保存将其发送到VC1中的集合视图,一切都会顺利运行。

然而,我有一个从谷歌下载的图像(png格式),并添加到我的xcode项目中,如果用户在VC2中没有实际选择图片,则该项目代表一个人的占位符。

问题是,在我的图像视图中的VC2中,我将图像视图设置为placeholderImage.png,对于VC1 collectionView cell imageView。

现在在VC2中,如果用户没有选择图像并继续回到VC1图像显示为placeholderImage.png,因为我在故事板中添加了这种方式..

但是当我退出应用程序或注销时,请回来。我的应用程序崩溃,因为我从未实际设置图像。图像返回零,它不会允许。

如果在VC2中没有选择图像,如何将VC1集合视图单元格imageView中的图像设置为placeholderImage.png文件?

谢谢。

    import UIKit
    import Foundation
    import Parse

    class TrainerArray: NSObject, NSCoding,     UIImagePickerControllerDelegate {

var name = ""
var trainerImage = UIImage()


func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(name, forKey: "Name")
    aCoder.encodeObject(trainerImage, forKey: "TrainerImage")
}

required init?(coder aDecoder: NSCoder) {
    name = aDecoder.decodeObjectForKey("Name") as! String
    trainerImage = (aDecoder.decodeObjectForKey("TrainerImage") as! UIImage)
    super.init()
}

override init() {
    super.init()
}


}

doneButton的代码:

    //ADDING TRAINER
   } else if trainerPhoto.image != nil {
        let alert = UIAlertController(title: "To Add New Trainers", message: "Enter Username to continue", preferredStyle: UIAlertControllerStyle.Alert)

        alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
        }

        alert.addAction(UIAlertAction(title: "Add Trainer", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
            let textF = alert.textFields![0] as UITextField

            if textF.text! != PFUser.currentUser()?.username {
                self.displayAlert("Incorrect Username!", message: "Please enter a valid username")

            } else if textF.text! == PFUser.currentUser()?.username && self.trainerPhoto.image != nil {
                let item = TrainerArray()

                let trainer = PFObject(className: "Trainer")
                trainer["name"] = self.textField.text
                trainer["userWhoPosted"] = PFUser.currentUser()!.objectId!

                let imageData = UIImagePNGRepresentation(self.trainerPhoto.image!)
                let imageFile = PFFile(name: "image.png", data: imageData!)

                trainer["trainerImage"] = imageFile
                trainer.saveInBackgroundWithBlock({ (success, error) -> Void in
                    if error == nil {
                        print("Successful save")
            }
            })
                item.name = self.textField.text!
                item.trainerImage = self.trainerPhoto.image!
                self.delegate?.addNewTrainerViewController(self, didFinishAddingItem: item)

            }



        }))

        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
            if action == true {
                self.dismissViewControllerAnimated(false, completion: nil)
            }}))

        self.presentViewController(alert, animated: true, completion: nil)

    }

0 个答案:

没有答案