我在控制台中收到以下错误:
终止应用程序未捕获的异常'NSInvalidArgumentException',原因:'-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象插入nil对象1 https://i.stack.imgur.com/SRrzd.png'
当我选择要添加到 firebase 的图像时,出现此错误。这发生在下面的代码中。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
// let imageToUse = PhotoArray()
// let data = UIImagePNGRepresentation(userPickedImage) //here convert to data
PhotoArray.sharedInstance.photosArray.append(userPickedImage) //append converted data in array
imageView.image = userPickedImage
//-----------------------------//
// //begin code from firebase docs
// Create a root reference
let storageRef = Storage.storage().reference()
// Create a reference to "mountains.jpg"
let ImgRef = storageRef.child("ImgRef.jpg")
// Create a reference to 'images/mountains.jpg'
let userImagesRef = storageRef.child("images/userImagesRef.jpg")
// While the file names are the same, the references point to different files
ImgRef.name == userImagesRef.name; // true
ImgRef.fullPath == userImagesRef.fullPath; // false
// Local file you want to upload
let localFile = URL(string: "path/to/image")!
// Create the file metadata
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
// Upload file and metadata to the object 'images/mountains.jpg'
let uploadTask = storageRef.putFile(from: localFile, metadata: metadata)
// Listen for state changes, errors, and completion of the upload.
uploadTask.observe(.resume) { snapshot in
// Upload resumed, also fires when the upload starts
}
uploadTask.observe(.pause) { snapshot in
// Upload paused
}
uploadTask.observe(.progress) { snapshot in
// Upload reported progress
let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
/ Double(snapshot.progress!.totalUnitCount)
}
uploadTask.observe(.success) { snapshot in
// Upload completed successfully
}
uploadTask.observe(.failure) { snapshot in
if let error = snapshot.error as? NSError {
switch (StorageErrorCode(rawValue: error.code)!) {
case .objectNotFound:
// File doesn't exist
break
case .unauthorized:
// User doesn't have permission to access file
break
case .cancelled:
// User canceled the upload
break
/* ... */
case .unknown:
// Unknown error occurred, inspect the server response
break
default:
// A separate error occurred. This is a good place to retry the upload.
break
}
}
}
imagePicker.dismiss(animated: true, completion: nil)
}
该错误似乎发生在第 76 行,如下所示。
您使用了错误的 API。URL(string
用于以 a 开头的 URL 字符串scheme
(http://
, ftp://
, file://
).
对于本地文件系统中的文件,您必须使用URL(fileURLWithPath:
它采用以以下开头的字符串路径/
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)