I am constructing an iOS app for my private use that analyzes my pictures on gadget utilizing a Core ML mannequin I educated. When fetching all of the photographs to research, it would not fetch all of them, as an alternative stopping round 1000 photographs, give or take 100. Why is that? Here is my code to assist debug:
class PhotoLibraryManager: ObservableObject {
@Printed var allAssets: [PHAsset] = []
@Printed var selectedImage: UIImage? = nil
@Printed var isSheetPresented = false
@AppStorage("photoCountLimit") var countLimit = true
non-public var thumbnailCache = NSCache<NSString, UIImage>()
init() {
fetchPhotoLibrary()
}
// MARK: - Fetch Picture Library
func fetchPhotoLibrary() {
PHPhotoLibrary.requestAuthorization { [weak self] standing in
guard standing == .licensed else {
print("Picture library entry denied")
return
}
DispatchQueue.international(qos: .userInitiated).async {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
// solely set fetch restrict if countLimit is true
if self?.countLimit == true {
fetchOptions.fetchLimit = 100
}
// fetch all picture property
let property = PHAsset.fetchAssets(with: .picture, choices: fetchOptions)
let assetsArray = property.objects(at: IndexSet(0..<property.rely))
// replace the printed property on the principle thread
DispatchQueue.fundamental.async {
self?.allAssets = assetsArray
print("Fetched (assetsArray.rely) property.")
}
}
}
}
// MARK: - Fetch Thumbnail
func fetchThumbnail(for asset: PHAsset, targetSize: CGSize, completion: @escaping (UIImage?) -> Void) {
let cacheKey = NSString(string: asset.localIdentifier)
if let cachedImage = thumbnailCache.object(forKey: cacheKey) {
completion(cachedImage)
return
}
let choices = PHImageRequestOptions()
choices.isSynchronous = false
choices.deliveryMode = .opportunistic
choices.isNetworkAccessAllowed = true
PHImageManager.default().requestImage(
for: asset,
targetSize: targetSize,
contentMode: .aspectFill,
choices: choices
) { [weak self] picture, _ in
if let picture = picture {
self?.thumbnailCache.setObject(picture, forKey: cacheKey)
}
completion(picture)
}
}
// MARK: - Fetch Full Picture
func fetchFullImage(for asset: PHAsset, completion: @escaping (UIImage?) -> Void) {
let choices = PHImageRequestOptions()
choices.isSynchronous = false
choices.deliveryMode = .highQualityFormat
choices.isNetworkAccessAllowed = true
PHImageManager.default().requestImage(
for: asset,
targetSize: PHImageManagerMaximumSize,
contentMode: .aspectFit,
choices: choices
) { picture, _ in
DispatchQueue.fundamental.async {
completion(picture)
}
}
}
// MARK: - Analyze Photographs
func analyzeAllImages(completion: @escaping ([String]) -> Void) {
DispatchQueue.international(qos: .userInitiated).async {
var outcomes: [String] = []
let group = DispatchGroup()
for asset in self.allAssets {
group.enter()
self.fetchAnalyzeImage(for: asset) { [weak self] picture in
if let picture = picture, let prediction = self?.analyzeImage(picture: picture) {
outcomes.append(prediction)
}
group.go away()
}
}
group.notify(queue: .fundamental) {
completion(outcomes)
}
}
}
func fetchAnalyzeImage(for asset: PHAsset, completion: @escaping (UIImage?) -> Void) {
let choices = PHImageRequestOptions()
choices.isSynchronous = false
choices.deliveryMode = .fastFormat
choices.isNetworkAccessAllowed = true
PHImageManager.default().requestImage(
for: asset,
targetSize: CGSize(width: 300, top: 300),
contentMode: .aspectFit,
choices: choices
) { picture, _ in
completion(picture)
}
}
func analyzeImage(picture: UIImage) -> String? {
do {
guard let pixelBuffer = picture.toPixelBuffer(dimension: CGSize(width: 360, top: 360)) else {
print("Didn't create pixel buffer")
return nil
}
let mannequin = strive NovaClassifier19f(configuration: MLModelConfiguration())
let output = strive mannequin.prediction(picture: pixelBuffer)
return output.goal
} catch {
print("Error analyzing picture: (error)")
return nil
}
}
}
Thanks for the assistance prematurely!! (Let me know if I would like to enhance on something in my query asking, I’m extraordinarily new to Stack Overflow.)