【问题标题】:How to display images from Photos (original photo gallery in ios)如何显示照片中的图像(ios中的原始照片库)
【发布时间】:2020-08-11 03:21:29
【问题描述】:

我创建了一个应用程序来显示照片中的所有图像(不是从照片中选择图像)。例如,谷歌照片可以访问照片中的所有图像,并且可以在其应用程序上再次显示。有谁知道使用 swift 在我们的应用程序上重新显示照片中的所有图像?

【问题讨论】:

标签: swift xcode storyboard swiftui xcode-storyboard


【解决方案1】:

请尝试可能的方法。在 xCode 中测试:11.2.1

注意:info.plist中添加Privacy - Photo Library Usage Description,以获取从图库访问照片的权限

import SwiftUI
import Photos

struct ContentView: View {
    @ObservedObject var photos = PhotosModel()
    var body: some View {
        List(photos.allPhotos, id: \.self) { photo in
            Image(uiImage: photo)
                .resizable()
                .frame(width: 200, height: 200, alignment: .center)
                .aspectRatio(1, contentMode: .fit)
        }
        .alert(isPresented: .constant(self.photos.errorString != "") ) {
            Alert(title: Text("Error"), message: Text(self.photos.errorString ), dismissButton: Alert.Button.default(Text("OK")))
        }
    }
}

class PhotosModel: ObservableObject {

    @Published var allPhotos = [UIImage]()
    @Published var errorString : String = ""

    init() {
        PHPhotoLibrary.requestAuthorization { (status) in
            switch status {
            case .authorized:
                self.errorString = ""
                self.getAllPhotos()
            case .denied, .restricted:
                self.errorString = "Photo access permission denied"
            case .notDetermined:
                self.errorString = "Photo access permission not determined"
            @unknown default:
                fatalError()
            }
        }
    }

    fileprivate func getAllPhotos() {

        let manager = PHImageManager.default()
        let requestOptions = PHImageRequestOptions()
        requestOptions.isSynchronous = false
        requestOptions.deliveryMode = .highQualityFormat
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

        let results: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
        if results.count > 0 {
            for i in 0..<results.count {
                let asset = results.object(at: i)
                let size = CGSize(width: 700, height: 700) //You can change size here
                manager.requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: requestOptions) { (image, _) in
                    if let image = image {
                        self.allPhotos.append(image)
                    } else {
                        print("error asset to image")
                    }
                }
            }
        } else {
            self.errorString = "No photos to display"
        }
    }
}

【讨论】:

  • 你好。非常感谢你帮助我。我已经在我的 xcode 项目中尝试了你的代码。这是我的错误。它是这样说的。 " CrashOnLaunchError: importPhotos 应用程序在启动时崩溃 更新预览时应用程序崩溃。在 ~/Library/Logs/DiagnosticReports 中查找崩溃日志以了解更多详细信息。"
猜你喜欢
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多