gpt4 book ai didi

ios - 图像选择器 ViewController 应用卡住 - NSXPCConnection

转载 作者:行者123 更新时间:2023-12-05 06:34:32 27 4
gpt4 key购买 nike

当我在设置中更改库的权限设置后使用图像选择器选择图像时。

我收到操作系统的异常。应用程序卡住了。甚至没有触发 didPickImageMedia 委托(delegate)。

2018-05-07 11:08:04.413510+0530 PersonalCare[1369:75557] [general] <NSXPCConnection: 0x1c41153c0> connection from pid 1374: Warning:

Exception caught during invocation of received message, dropping incoming message and invalidating the connection. Exception: * -[NSURL URLByAppendingPathExtension:]: component, components, or pathExtension cannot be nil. * -[NSURL URLByAppendingPathExtension:]: component, components, or pathExtension cannot be nil. ( 0 CoreFoundation 0x000000018423bd50 + 148 1 libobjc.A.dylib 0x0000000183750528 objc_exception_throw + 56 2 CoreFoundation 0x000000018423bc80 + 0 3 Foundation 0x0000000184c4d91c + 92 4 PhotosUI 0x000000019707d4a0 + 1660 5 PhotosUI 0x000000019707c92c + 36 6 Foundation 0x0000000184d501e0 + 20 7 Foundation 0x0000000184d4e97c + 2632 8 Foundation 0x0000000184b25830 + 240 9 libxpc.dylib 0x0000000183ea2300 + 68 10 libxpc.dylib 0x0000000183e9fcb0 + 984 11 libdispatch.dylib 0x000000010371152c _dispatch_client_callout4 + 16 12 libdispatch.dylib 0x0000000103714f58 _dispatch_mach_msg_invoke + 380 13 libdispatch.dylib 0x000000010371ff30 _dispatch_queue_serial_drain + 212 14 libdispatch.dylib 0x00000001037144c0 _dispatch_mach_invoke + 992 15 libdispatch.dylib 0x000000010371ff30 _dispatch_queue_serial_drain + 212 16 libdispatch.dylib 0x00000001037149a4 _dispatch_queue_invoke + 332 17 libdispatch.dylib 0x0000000103721104 _dispatch_root_queue_drain_deferred_wlh + 424 18 libdispatch.dylib 0x0000000103728100 _dispatch_workloop_worker_thread + 652 19 libsystem_pthread.dylib 0x0000000183e66fe0 _pthread_wqthread + 932 20 libsystem_pthread.dylib 0x0000000183e66c30 start_wqthread + 4 )

我的代码

extension ProfileViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
buttonImage.contentMode = .scaleAspectFit
let imageData: Data = UIImageJPEGRepresentation(pickedImage, 0.7)!
encoded = imageData.base64EncodedString(options: .endLineWithCarriageReturn)
profileImage = pickedImage
buttonImage.setBackgroundImage(pickedImage, for: .normal)
} else if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
buttonImage.contentMode = .scaleAspectFit
let imageData: Data = UIImageJPEGRepresentation(pickedImage, 0.7)!
profileImage = pickedImage
encoded = imageData.base64EncodedString(options: .endLineWithCarriageReturn)
buttonImage.setBackgroundImage(pickedImage, for: .normal)
}
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}

最佳答案

因为UIImagePickerViewController的很多功能在以后的版本中(iOS14新版本)会被移除。你应该使用 PHPickerViewController

import PhotosUI
if #available(iOS 14, *) {
// using PHPickerViewController
var config = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
config.selectionLimit = 1
config.filter = .images
config.preferredAssetRepresentationMode = .current
let picker = PHPickerViewController(configuration: config)
picker.delegate = self
DispatchQueue.main.async {
present(picker, animated: true, completion: nil)
}
} else {
let imagePickerViewController = UIImagePickerController()
imagePickerViewController.allowsEditing = false
imagePickerViewController.sourceType = .photoLibrary
imagePickerViewController.delegate = self
imagePickerViewController.modalPresentationStyle = .fullScreen
if(Device.isPad) {
imagePickerViewController.modalPresentationStyle = .popover
imagePickerViewController.popoverPresentationController?.sourceView = self.view
imagePickerViewController.popoverPresentationController?.sourceRect = self.view.frame
imagePickerViewController.popoverPresentationController?.permittedArrowDirections = .any
}
DispatchQueue.main.async {
present(picker, animated: true, completion: nil)
}
}
extension ViewController: PHPickerViewControllerDelegate {
@available(iOS 14, *)
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
dismiss(animated: true, completion: nil)
guard !results.isEmpty else { return }
// request image urls
let identifier = results.compactMap(\.assetIdentifier)
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: identifier, options: nil)
var count = fetchResult.count
fetchResult.enumerateObjects {(asset, index, stop) in
PHAsset.getURL(ofPhotoWith: asset) {[weak self] (url) in
if let url = url {
// got image url
} else {
// show error
}
}
}
}
}

extension PHAsset {
static func getURL(ofPhotoWith mPhasset: PHAsset, completionHandler : @escaping ((_ responseURL : URL?) -> Void)) {
if mPhasset.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
mPhasset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
if let fullSizeImageUrl = contentEditingInput?.fullSizeImageURL {
completionHandler(fullSizeImageUrl)
} else {
completionHandler(nil)
}
})
} else if mPhasset.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestAVAsset(forVideo: mPhasset, options: options, resultHandler: { (asset, audioMix, info) in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl = urlAsset.url
completionHandler(localVideoUrl)
} else {
completionHandler(nil)
}
})
}

}
}

关于ios - 图像选择器 ViewController 应用卡住 - NSXPCConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50207748/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com