Skip to content

Instantly share code, notes, and snippets.

@Yihwan
Created March 29, 2020 18:07
Show Gist options
  • Save Yihwan/902287db7cf967cab658148bb3419d0a to your computer and use it in GitHub Desktop.
Save Yihwan/902287db7cf967cab658148bb3419d0a to your computer and use it in GitHub Desktop.
class PhotoManager: ObservableObject {
private func loadImage() -> Image? {
let manager = PHImageManager.default()
let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions())
var image: UIImage? = nil
manager.requestImage(for: fetchResult.object(at: 0), targetSize: CGSize(width: 647, height: 375), contentMode: .aspectFill, options: requestOptions()) { img, err in
guard let img = img else { return }
image = img
}
return Image(uiImage: image!)
}
private func fetchOptions() -> PHFetchOptions {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
return fetchOptions
}
private func requestOptions() -> PHImageRequestOptions {
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
return requestOptions
}
}
struct PhotoGrid: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UICollectionViewController {
let collectionViewController = UICollectionViewController()
collectionViewController.collectionView.dataSource = context.coordinator
return collectionViewController
}
func updateUIViewController(_ uiViewController: UICollectionViewController, context: Context) {
<#code#>
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UICollectionViewDataSource {
var parent: PhotoGrid
init(_ parent: PhotoGrid) {
self.parent = parent
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment