来自相机的UIImagePickerControllerOriginalImage没有在ios swift中调用

时间:2018-04-12 11:24:45

标签: ios swift xcode uiimageview uiimagepickercontroller

我可以从图库中选择图片并保存到UIImageView但是在尝试相机时我无法将图像保存在UIImageView中。它会调用didFinishPickingMediaWithInfo方法,但如果是这样的话会进入UIImagePickerControllerOriginalImage

这是我用于相机和图库选取器的代码:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {



    if let pickedImage = info[UIImagePickerControllerImageURL] as? URL {

        if let data : Data = try? Data(contentsOf: pickedImage as URL)
        {

            print(data.format)

            if data.format == "gif" {

                let data = try? Data(contentsOf: pickedImage) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
                let imageView = UIImage(data: data!)
                let width = imageView?.size.width
                let height = imageView?.size.height

                let url = SaveItemFilemanager.sharedinstance.saveGifimage(data: data!)

                addGifImage(url: url!, width: width!, height: height!)

            } else {

                if let photo = info[UIImagePickerControllerLivePhoto]  as? PHLivePhoto {

                    let assetResources = PHAssetResource.assetResources(for: photo)

                    let asset = assetResources[1] 

                    let url = SaveItemFilemanager.sharedinstance.saveLiveimage(data: asset)

                    print(url!)

                    let frameCount = 16
                    let delayTime  = Float(0.2)
                    let loopCount  = 0

                    let width = photo.size.width
                    let height = photo.size.height

                    let regift : Regift = Regift(sourceFileURL: url!, frameCount: frameCount, delayTime: delayTime, loopCount: loopCount)

                    addGifImage(url: regift.createGif()!, width: width, height: height)



                } else if let normalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {



                    let width = normalImage.size.width
                    let height = normalImage.size.height

                    let url = SaveItemFilemanager.sharedinstance.saveimage(image: normalImage)

                    addImage(url: url!, width: width, height: height)

                } else {

                    let normalImage = info[UIImagePickerControllerOriginalImage] as? UIImage
                    let width = normalImage?.size.width
                    let height = normalImage?.size.height

                    let url = SaveItemFilemanager.sharedinstance.saveimage(image: normalImage!)

                    addImage(url: url!, width: width!, height: height!)

                }
          }

        }

    }

    dismiss(animated: true, completion: nil)


}

  func showCamera() {

    let imagePicker = UIImagePickerController()

    if(UIImagePickerController .isSourceTypeAvailable(.camera))
    {

       imagePicker.delegate = self
       imagePicker.sourceType = .camera


    }
    present(imagePicker, animated: true, completion: nil)
}

我甚至通过在其所称的完成的媒体中保留断点来检查并立即解雇。

0 个答案:

没有答案