从相机胶卷中取出一张图像后不显示图像,将输入的图像存储在阵列中

时间:2015-06-28 21:33:44

标签: ios arrays xcode swift uiimage

好。首先,对不起,我很抱歉。我的问题:我有代码在相机胶卷中搜索图像或取一个,然后将其存储在UIImage中。但是,它不会在NewPersonViewController上显示图像。其次,我想将保存的图片image附加到我创建的空UIImages数组中,但我不知道如何。我的代码片段:

var image:UIImage?
var Images:[UIImage] = []
//new snippet
Images.append(image!)
//another snippet
func updateDisplay(){
        if let mediaType = lastChosenMediaType{
            if mediaType == kUTTypeImage as NSString{
                imageView.image = image!
                imageView.hidden = false
                if moviePlayerController != nil{
                    moviePlayerController!.view.hidden = true
                }
            }else if mediaType == kUTTypeMovie as NSString{
                if moviePlayerController == nil{
                    moviePlayerController = MPMoviePlayerController(contentURL: movieURL)
                    let movieView = moviePlayerController!.view
                    movieView.frame = imageView.frame
                    movieView.clipsToBounds = true
                    view.addSubview(movieView)
            }
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    lastChosenMediaType = info[UIImagePickerControllerMediaType] as? String
    if let mediaType = lastChosenMediaType{
        if mediaType == kUTTypeImage as NSString{
            image = info[UIImagePickerControllerEditedImage] as? UIImage
        }
        else if mediaType == kUTTypeMovie as NSString{
            movieURL = info[UIImagePickerControllerMediaURL] as? NSURL
        }
    }
    picker.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    picker.dismissViewControllerAnimated(true, completion: nil)
}
    }

Images.append(image!)编译,但会出现此错误:fatal error: unexpectedly found nil while unwrapping an Optional value.

真的很感激:)旁注:我是swift的新手。

1 个答案:

答案 0 :(得分:0)

尝试将图像附加到数组中时,请尝试将图像附加到数组中。

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    lastChosenMediaType = info[UIImagePickerControllerMediaType] as? String
    if let mediaType = lastChosenMediaType{
       if mediaType == kUTTypeImage as NSString{
          let pickImg = info[UIImagePickerControllerEditedImage] as? UIImage
             Images.append(pickImg)
             imageView.image = pickImg //set the imageView image.
      }
      else if mediaType == kUTTypeMovie as NSString{
          movieURL = info[UIImagePickerControllerMediaURL] as? NSURL
      }
  } 
 picker.dismissViewControllerAnimated(true, completion: nil)
}

这是从我的项目中获取的代码。

 var newImage : UIImage?
 var Images:[UIImage] = []

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

    let mediaType = info[UIImagePickerControllerMediaType] as String
    if mediaType == "public.image" {
        // Media is an image
        newImage = info[UIImagePickerControllerOriginalImage] as UIImage!
        println("update imgPreview")
        imgPreview.image = newImage
        Images.append(newImage!)

    } else if mediaType == "public.movie" {

    }

    self.dismissViewControllerAnimated(true, completion: nil)
}

这是我如何调用图像选择器

func pickPhoto() {

    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.mediaTypes = [kUTTypeImage as NSString]
    //imagePicker.allowsEditing = true

    self.presentViewController(imagePicker, animated: true,
        completion: nil)
}