在我的项目中,我向用户显示了库图像和视频,但是在某些设备中,我遇到了ArrayBuffer.getElementSlowPath
之类的崩溃。谁能指导我如何重现此问题?我从Crashlytics那里得到了这个问题。
这是我从节拍中获取视频的代码。
func getVideo(withCompletionHandler completion:@escaping CompletionHandler) {
let fetchOptions = PHFetchOptions()
let requestOptions = PHVideoRequestOptions()
requestOptions.isNetworkAccessAllowed = false
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)]
let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.video, options: fetchOptions)
fetchResult.enumerateObjects ({ (assest, index, isCompleted) in
if assest.sourceType != PHAssetSourceType.typeiTunesSynced{
PHImageManager.default().requestAVAsset(forVideo: assest , options: requestOptions, resultHandler: { (asset : AVAsset?, video : AVAudioMix?, dic : [AnyHashable : Any]?) in
if let _ = asset as? AVURLAsset
{
let objAssest = GallaryAssets()
objAssest.objAssetsType = assetsType.videoType
objAssest.createdDate = (assest ).creationDate
objAssest.assetsDuration = (assest ).duration
objAssest.assetsURL = (asset as! AVURLAsset).url
objAssest.localizationStr = assest.localIdentifier
objAssest.locationInfo = LocationInfo()
if let location = (assest).location
{
objAssest.locationInfo.Latitude = "\(location.coordinate.latitude)"
objAssest.locationInfo.Longitude = "\(location.coordinate.longitude)"
}
self.media.add(objAssest)
}
completion(self.media)
}
})
}
})
}
}
答案 0 :(得分:1)
您要删除completion
上的控制器吗? enumerateObject
函数是同步执行的,如果在完成函数停止执行之前调用完成函数,并且fetchResult
对象在函数结束执行之前被释放,则可能导致问题。
尝试在枚举结束后调用完成块,如果只需要一个结果,请将isCompleted
指针更改为true,这将停止执行枚举:
func getVideo(withCompletionHandler completion:@escaping CompletionHandler) {
let fetchOptions = PHFetchOptions()
let requestOptions = PHVideoRequestOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)]
let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.video, options: fetchOptions)
fetchResult.enumerateObjects ({ (assest, index, isCompleted) in
if assest.sourceType != PHAssetSourceType.typeiTunesSynced{
PHImageManager.default().requestAVAsset(forVideo: assest , options: requestOptions, resultHandler: { (asset : AVAsset?, video : AVAudioMix?, dic : [AnyHashable : Any]?) in
if let _ = asset as? AVURLAsset
{
let objAssest = GallaryAssets()
objAssest.objAssetsType = assetsType.videoType
objAssest.createdDate = (assest ).creationDate
objAssest.assetsDuration = (assest ).duration
objAssest.assetsURL = (asset as! AVURLAsset).url
objAssest.localizationStr = assest.localIdentifier
objAssest.locationInfo = LocationInfo()
if let location = (assest).location
{
objAssest.locationInfo.Latitude = "\(location.coordinate.latitude)"
objAssest.locationInfo.Longitude = "\(location.coordinate.longitude)"
}
self.media.add(objAssest)
isCompleted.pointee = true
}
})
}
})
completion(self.media)
}
答案 1 :(得分:1)
可能是某些视频/资源来自iCloud,您需要删除 PHVideorequestoptions ,然后检查流量。希望您的崩溃能解决。
func getVideo(withCompletionHandler completion:@escaping CompletionHandler) {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)]
let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.video, options: fetchOptions)
fetchResult.enumerateObjects ({ (assest, index, isCompleted) in
if assest.sourceType != PHAssetSourceType.typeiTunesSynced{
PHImageManager.default().requestAVAsset(forVideo: assest , options: **PHVideoRequestOptions**(), resultHandler: { (asset : AVAsset?, video : AVAudioMix?, dic : [AnyHashable : Any]?) in
if let _ = asset as? AVURLAsset
{
let objAssest = GallaryAssets()
objAssest.objAssetsType = assetsType.videoType
objAssest.createdDate = (assest ).creationDate
objAssest.assetsDuration = (assest ).duration
objAssest.assetsURL = (asset as! AVURLAsset).url
objAssest.localizationStr = assest.localIdentifier
objAssest.locationInfo = LocationInfo()
if let location = (assest).location
{
objAssest.locationInfo.Latitude = "\(location.coordinate.latitude)"
objAssest.locationInfo.Longitude = "\(location.coordinate.longitude)"
}
self.media.add(objAssest)
isCompleted.pointee = true
}
})
}
})
completion(self.media)
}
答案 2 :(得分:1)
我不能肯定地说这是导致崩溃的原因,但是对于在Google搜索结果中发现此问题的其他人来说:
我在Crashlytics中使用ArrayBuffer.getElementSlowPath遇到了类似的隐秘堆栈跟踪。经过大量调试后,我在本地重现了崩溃,结果发现一个类型化的Swift数组中包含一个不是预期类型的元素。发生这种情况是因为所讨论的数组实际上是从Objective-C桥接的,而Objective-C对数组的类型的要求不严格。