设置:
\n我用来PHPickerViewController允许用户选择照片。我已经实现了PHPickerViewControllerDelegate协议方法picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])。itemProvider我从结果数组中获取。
问题:
\n我需要访问所选图像的数据。NSItemProviderCalled上有一个方法loadDataRepresentation(),它接受类型标识符字符串,并应在其完成块中返回图像数据。
当我调用这个方法时,有时它会返回数据,有时则不会。我已经对每个提供程序的多个注册类型标识符进行了尝试,并且看到了同样的情况。有时它会返回 JPEG 数据,但不返回 HEIF,有时仅返回 HEIF,有时根本不返回任何数据。这是一致的,因为每次我尝试加载其数据时,同一张照片都会显示相同的结果。
\n当没有数据时,我看到以下错误:
\nUpload preparation for claim D8C281B7-CCCE-4299-95BF-9355FCF340E4 completed with error: Error Domain=NSCocoaErrorDomain Code=260 "The file \xe2\x80\x9c7AB1AB3F-7643-45C1-9DD9-5F5642965C3A.jpeg\xe2\x80\x9d couldn\xe2\x80\x99t be opened because there is no such file."\nRun Code Online (Sandbox Code Playgroud)\n在尝试加载照片数据之前,我需要对照片执行某些操作吗?也许我在这里错过了一步?我直接用 iPhone XR 拍摄的照片会出现这种情况。我没有使用 iCloud 照片,所有照片都在设备上。
\n所以,我发现之前问过类似的问题,但是当我尝试与我得到的方法相同的方法时,它没有按我的预期工作,所以我想做的是,我想从 PHPickerViewController 的 PHPickerResult 获取 PHAsset。
因此,使用此源代码作为我的基本代码进行实验,并将其与我从中获得的内容相结合。
info.plist当我尝试此操作时,还已经添加了“隐私 - 照片库使用说明” 。
代码如下所示。
import UIKit
import PhotosUI
class ViewController: UIViewController {
@IBOutlet weak var myImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonDidTap(_ sender: Any) {
var configuration = PHPickerConfiguration()
configuration.selectionLimit = 1
configuration.filter = .any(of: [.images, .videos])
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
self.present(picker, animated: true, completion: nil)
}
}
extension ViewController: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) …Run Code Online (Sandbox Code Playgroud) 我用于PHPickerViewController在 iOS 15 中选择用于用户个人资料图片目的的图像。我正在使用 UIKit 框架。我有以下代码:
var pickerConfig = PHPickerConfiguration(photoLibrary: .shared())
pickerConfig.selectionLimit = 1
pickerConfig.filter = .images
let pickerView = PHPickerViewController(configuration: pickerConfig)
pickerView.delegate = self
self.present(pickerView, animated: true)
Run Code Online (Sandbox Code Playgroud)
选择器工作正常,可以选择图像并委派结果。但是,当Cancel按下按钮时,没有任何反应,选择器也没有按预期关闭。
当按下实例PHPickerViewController自己的按钮时如何关闭实例?Cancel
编辑:
Method的实现PHPickerViewControllerDelegate如下:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
{
results.first?.itemProvider.loadObject(ofClass: UIImage.self) { [unowned self] reading , error in
guard let image = reading as? UIImage, error == nil else
{
DispatchQueue.main.async {
picker.dismiss(animated: true)
self.profilePictureHasError = true
self.toggleDoneButtonEnabled()
}
return …Run Code Online (Sandbox Code Playgroud) 我有一个 PHPIckerViewController,自 iOS 14 起可用。我想从画廊获取 WEBP 格式的图像。但是 PHPicker 中的项目提供程序无法加载这种格式的图像。请告诉我如何使用新的选择器在 UIButton 上选择和设置图像。
代码:
extension SixStepRegistrationViewController: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
let supportedRepresentations = [UTType.rawImage.identifier,
UTType.tiff.identifier,
UTType.bmp.identifier,
UTType.png.identifier,
UTType.jpeg.identifier,
UTType.webP.identifier,
]
for representation in supportedRepresentations {
if results[0].itemProvider.hasRepresentationConforming(toTypeIdentifier: representation, fileOptions: .init()) {
print(representation, " repr")
results[0].itemProvider.loadInPlaceFileRepresentation(forTypeIdentifier: representation) { (originalUrl, inPlace, error) in
DispatchQueue.main.async {
print(originalUrl, " ", inPlace)
self.addPhotoButton.setImage(UIImage(contentsOfFile: originalUrl!.path), for: .normal)
//self.dismiss(animated: true, completion: nil)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
谢谢
目前,我正在尝试深入研究 PHPickerViewController 以从照片中同时选择多个图像。所以我想要用户选择的图像数组,我尝试了太多方法但没有运气。这是我的代码,请告诉我有最佳实践吗?
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true, completion: nil)
var images: [UIImage?] = []
var totalConversionsCompleted = 0
for (index,result) in results.enumerated() {
result.itemProvider.loadObject(ofClass: UIImage.self, completionHandler: { (object, error) in
let image = object as? UIImage
images.append(image)
totalConversionsCompleted += 1
if totalConversionsCompleted == index {
print("completion happen \(images)")
}
})
}
}
Run Code Online (Sandbox Code Playgroud)