关闭相机并返回应用程序需要很长时间
Posted
技术标签:
【中文标题】关闭相机并返回应用程序需要很长时间【英文标题】:dismissing camera and returning to app takes a long time 【发布时间】:2017-11-07 14:09:33 【问题描述】:我有一个应用程序可以让用户添加他的图像。为此,我使用 UIImagePicker。
请看下面的函数: 当点击图像时,它会以弹出窗口的形式打开一个视图。
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
let userDetails:Dictionary = (UserDefaults.standard.value(forKey: "myUserDetails") as? [String:Any])!
let UserID:Int = userDetails["UserID"] as! Int
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "imager") as! imagerVC
popOverVC.UserID = UserID
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
popOverVC.callback = image in
// do something with the image
self.profile_image.image = image
if let data = UIImagePNGRepresentation(image)
//save profile image as NewUserID
UserDefaults.standard.set(data, forKey: String(UserID))
UserDefaults.standard.synchronize()
这是激活相机的视图代码,该视图在上面的代码中以 popOverVC 的形式打开。
import UIKit
class imagerVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
var UserID:Int = 0
@IBOutlet weak var myImageView: UIImageView!
var callback : ((UIImage) -> ())?
@IBOutlet weak var btn_end: UIButton!
@IBOutlet weak var from_camera: UIBarButtonItem!
@IBOutlet weak var from_gallery: UIBarButtonItem!
@IBAction func btn_end_pressed(_ sender: UIButton)
self.view.removeFromSuperview()
@IBAction func btn_closer(_ sender: UIButton)
self.view.removeFromSuperview()
@IBAction func photofromLibrary(_ sender: UIBarButtonItem)
picker.allowsEditing = false
picker.sourceType = .photoLibrary
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
picker.modalPresentationStyle = .popover
present(picker, animated: true, completion: nil)
picker.popoverPresentationController?.barButtonItem = sender
@IBAction func shootPhoto(_ sender: UIBarButtonItem)
if UIImagePickerController.isSourceTypeAvailable(.camera)
UserDefaults.standard.set(true, forKey: "is_selfie")
UserDefaults.standard.synchronize()
DispatchQueue.main.async
self.picker.allowsEditing = false
self.picker.sourceType = UIImagePickerControllerSourceType.camera
self.picker.cameraCaptureMode = .photo
self.picker.modalPresentationStyle = .fullScreen
self.present(self.picker,animated: true,completion: nil)
else
noCamera()
let picker = UIImagePickerController()
override func viewDidLoad()
super.viewDidLoad()
picker.delegate = self
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
//MARK: - Delegates
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
myImageView.contentMode = .scaleAspectFit //3
myImageView.image = chosenImage //4
myImageView.layer.borderWidth = 1
myImageView.layer.masksToBounds = false
myImageView.layer.borderColor = UIColor.black.cgColor
myImageView.layer.cornerRadius = myImageView.frame.height/4
myImageView.clipsToBounds = true
callback?(chosenImage)
dismiss(animated:true, completion: nil)
func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
dismiss(animated: true, completion: nil)
func noCamera()
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .alert)
let okAction = UIAlertAction(
title: "OK",
style:.default,
handler: nil)
alertVC.addAction(okAction)
present(
alertVC,
animated: true,
completion: nil)
我有两个问题。也许他们是相关的。 主要问题是在相机视图加载并拍摄照片后,当我按下按钮使用照片时,相机不会立即关闭,并且在相机视图被关闭之前大约有一分钟的延迟。顺便说一下,一旦按下“使用照片”按钮,就会触发回调函数。
我不确定这是否已连接,但我收到以下警告: 实例方法 'imagePickerController(_:didFinishPickingMediaWithInfo:)' 几乎符合要求,但当我让 Xcode 自动更正我时,相机功能停止运行。
我试着换行 解雇(动画:真,完成:无) 在 DispatchQueue.main.async 中使其在主线程上运行,但这不起作用
不知道为什么,但物理 iPad 没有延迟,只有 iPhone 设备
非常感谢您提供的任何帮助
【问题讨论】:
imageTapped 函数在哪里调用?那可能在后台线程中吗? 【参考方案1】:这是模拟器上的预期行为。它不应该在设备上滞后,正如你所说的它在设备上工作然后它很酷。 !!!
【讨论】:
行为在物理设备上。 3 iphone 6以上是关于关闭相机并返回应用程序需要很长时间的主要内容,如果未能解决你的问题,请参考以下文章
为什么phpMyAdmin需要很长时间来显示查询,但是显示查询执行得很快? [关闭]
在 VS2010 中停止在 IIS 上调试 Web 应用程序需要很长时间
使用 OracleDataAccess.Fill(datatable) 更新 DataGridView 需要很长时间