英文:
delete photo after selection
问题
以下是你代码中需要翻译的部分:
所以,我有一个 SwiftUI 应用程序,允许用户解析所选图像的内容并将其保存供以后使用。这一部分已经完成。我想在解析完成后从用户的照片库中删除所选择的图片。
这是我的代码的简化版本:
struct NewItemView: View {
@State private var shouldDeleteImage = true
@State private var photosPickerItem: PhotosPickerItem?
var body: some View {
NavigationView {
Form {
Section("image") {
PhotosPicker(selection: $photosPickerItem) {
Label("选择 Duolingo 图片", systemImage: "photo")
}
}
.onChange(of: photosPickerItem) { selectedPhotosPickerItem in
guard let selectedPhotosPickerItem else {
return
}
debugPrint(selectedPhotosPickerItem)
Task {
isBeingParsed = true
await updatePhotosPickerItem(with: selectedPhotosPickerItem)
isBeingParsed = false
}
}
Section("解析的内容") {
// 在此处显示内容..
}
}
.navigationTitle("新项目")
}
}
private func updatePhotosPickerItem(with item: PhotosPickerItem) async {
photosPickerItem = item
if let photoData = try? await item.loadTransferable(type: Data.self) {
let image = UIImage(data: photoData)
// 处理图像..
// 这里出问题: itemIdentifier 总是为空
self.deleteImage(assetIdentifier: item.itemIdentifier!)
}
}
// 如何在这里获取 assetIdentifier
private func deleteImage(assetIdentifier: String) {
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [assetIdentifier], options: nil)
guard let asset = fetchResult.firstObject else { return }
PHPhotoLibrary.shared().performChanges {
PHAssetChangeRequest.deleteAssets([asset] as NSArray)
} completionHandler: { success, error in
if success {
print("从照片库中删除图片")
} else {
print("删除图片出错: \(error?.localizedDescription ?? "未知错误")")
}
}
}
}
请注意,我只翻译了代码部分,没有包括你的问题或其他内容。如果你需要更多帮助,请随时告诉我。
英文:
So, I have a SwiftUI app that lets the user parses content for a selected image and saves it for later use. The part is done. I want to remove the chosen picture from the user's photo library once the parsing is done.
Here's a simplified version of my code:
struct NewItemView: View {
@State private var shouldDeleteImage = true
@State private var photosPickerItem: PhotosPickerItem?
var body: some View {
NavigationView {
Form {
Section("image") {
PhotosPicker(selection: $photosPickerItem) {
Label("Select a Duolingo Photo", systemImage: "photo")
}
}
.onChange(of: photosPickerItem) { selectedPhotosPickerItem in
guard let selectedPhotosPickerItem else {
return
}
debugPrint(selectedPhotosPickerItem)
Task {
isBeingParsed = true
await updatePhotosPickerItem(with: selectedPhotosPickerItem)
isBeingParsed = false
}
}
Section("Parsed content") {
// show content here..
}
}
.navigationTitle("New item")
}
}
private func updatePhotosPickerItem(with item: PhotosPickerItem) async {
photosPickerItem = item
if let photoData = try? await item.loadTransferable(type: Data.self) {
let image = UIImage(data: photoData)
// process the image..
// 💥 it breaks here: itemIdentifier is always nil
self.deleteImage(assetIdentifier: item.itemIdentifier!)
}
}
// how to get the assetIdentifier here 😫
private func deleteImage(assetIdentifier: String) {
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [assetIdentifier], options: nil)
guard let asset = fetchResult.firstObject else { return }
PHPhotoLibrary.shared().performChanges {
PHAssetChangeRequest.deleteAssets([asset] as NSArray)
} completionHandler: { success, error in
if success {
print("Image deleted from photo library")
} else {
print("Error deleting image from photo library: \(error?.localizedDescription ?? "unknown error")")
}
}
}
}
Any idea how to get the asset identifier? Or any other workaround that would help achieve my goal is very welcome
答案1
得分: 1
你之所以得到 nil
的标识符,我认为是因为在创建 PhotosPicker
时你没有指定照片库。
例如:
PhotosPicker(selection: $photosPickerItem, matching: ..., photoLibrary: .shared()))
英文:
The reason, I think that you got nil
identifiers is because you haven't specified a photo library when you created the PhotosPicker
.
For example:
PhotosPicker(selection: $photosPickerItem, matching: ..., photoLibrary: .shared()))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论