Я создал проект, который использует Quikkly iOS SDK (https://github.com/quikkly/ios-sdk) для сканирования пользовательских штрих-кодов QR.
По сути, я хочу получить последние 20 изображений с устройства пользователя, добавитьотправьте их в массив и проанализируйте каждое изображение в массиве на предмет правильного «кода Quikkly» с помощью Quikkly SDK.
Однако каждый раз, когда я запускаю приложение, оно вылетает с ошибкой: Thread 4:EXC_BAD_ACCESS (код = 1, адрес = 0x113f78000) Когда приложение достигает этой строки: Scannable.detect (inImage: image.cgImage!) {(Scannables) в
Пожалуйста, см.мой код ниже:
import Foundation
import Photos
import UIKit
import Quikkly
class Codes{
//fetch last 20 images from the devices gallery
var images:[UIImage] = []
func fetchPhotos () {
// Sort the images by descending creation date and fetch the first 20
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)]
fetchOptions.fetchLimit = 20
// Fetch the image assets
let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
// If the fetch result isn't empty,
// proceed with the image request
if fetchResult.count > 0 {
let totalImageCountNeeded = 20 // <-- The number of images to fetch
fetchPhotoAtIndex(0, totalImageCountNeeded, fetchResult)
}
}
// Repeatedly call the following method while incrementing
// the index until all the photos are fetched
func fetchPhotoAtIndex(_ index:Int, _ totalImageCountNeeded: Int, _ fetchResult: PHFetchResult<PHAsset>){
// Note that if the request is not set to synchronous
// the requestImageForAsset will return both the image
// and thumbnail; by setting synchronous to true it
// will return just the thumbnail
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
// Perform the image request
PHImageManager.default().requestImage(for: fetchResult.object(at: index) as PHAsset, targetSize: CGSize(width: 200, height: 200) , contentMode: PHImageContentMode.aspectFill, options: requestOptions, resultHandler: { (image, _) in
if let image = image {
// Add the returned image to your array
self.images += [image]
}
// If you haven't already reached the first
// index of the fetch result and if you haven't
// already stored all of the images you need,
// perform the fetch request again with an
// incremented index
if index + 1 < fetchResult.count && self.images.count < totalImageCountNeeded {
self.fetchPhotoAtIndex(index + 1, totalImageCountNeeded, fetchResult)
} else {
// Else you have completed creating your array
//print("Completed array: \(self.images)")
print("Completed array ****")
self.scanImageQuikkly()
}
})
}
public func scanImageQuikkly(){
for image in self.images {
if image.cgImage != nil {
print("CGImage was not nil")
Scannable.detect(inImage: image.cgImage!) { (scannables) in
if let scannable = scannables.first {
print(scannable.value)
}else{
print("No code found")
}
}
}else{
print("CGImage was nil")
}
}
}
}
Это мой вывод:
2019-09-19 14:15:12.758551+0100 TestApp[1295:277458] [core] "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""
Completed array ****
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
CGImage was not nil
Любая помощь будет принята с благодарностью ... Я застрял на этом слишком долго :(