SWIFT ALAssets库没有枚举组 [英] SWIFT ALAssetsLibrary not enumerating groups
问题描述
我试图收集所有用户的图像缩略图到一个数组,但当我调用 enumerateAssetsUsingBlock
方法 ALAssetsLibrary
似乎没有发生。
I'm trying to gather thumbnails of all the user's images into an array, but when I call the enumerateAssetsUsingBlock
method of ALAssetsLibrary
nothing seems to happen.
import UIKit
import AssetsLibrary
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var photoLibView: UICollectionView
var assetLibrary : ALAssetsLibrary = ALAssetsLibrary()
func showCustomLibrary() {
self.assetLibrary = ALAssetsLibrary()
var assetsArray : [ALAsset] = []
var imageArray : [CGImage] = []
var count = 0
var countOne = 0
let assetsType : ALAssetsGroupType = Int(ALAssetsGroupAll)
var groupBlock : ALAssetsLibraryGroupsEnumerationResultsBlock = {
(group: ALAssetsGroup!, stop: UnsafePointer<ObjCBool>) in
println("is goin")
count++
var assetBlock : ALAssetsGroupEnumerationResultsBlock = {
(result: ALAsset!, index: Int, stop: UnsafePointer<ObjCBool>) in
imageArray.append(result.thumbnail().takeRetainedValue())
assetsArray.append(result)
countOne++
}
group.enumerateAssetsUsingBlock(assetBlock)
}
var groupFailureBlock : ALAssetsLibraryAccessFailureBlock = {
(NSError) in
println("errorrrrrrrr")
}
assetLibrary.enumerateGroupsWithTypes(assetsType, usingBlock: groupBlock, failureBlock: groupFailureBlock)
println("number of groups")
println(count)
println("number of total assets")
println(countOne)
self.photoLibView.insertItemsAtIndexPaths(imageArray)
}
}
当我运行showCustomLibrary被调用,编译器输出组数
0
总资产数b b $ b 0
致命错误:意外找到nil展开可选值
,因为它看起来好像 ALAssetsLibrary
的组未被枚举。 (is goin未打印)。任何想法在这里发生了什么?先感谢!
When I run showCustomLibrary() is called, the compiler prints number of groups
0
number of total assets
0
fatal error: unexpectedly found nil while unwrapping an Optional value
because it seems as though the groups of the ALAssetsLibrary
are not being enumerated. ("is goin" is not being printed). Any idea what's going on here? Thanks in advance!
推荐答案
我发现了问题。
在这个组和结果应该不是nil其他明智的它崩溃在swift。
所以我们应该检查nil case
Like
I found the issue. In this "group" and result should not be nil other wise it crash in swift. So we should check for nil case Like
var groupBlock : ALAssetsLibraryGroupsEnumerationResultsBlock = {
(group: ALAssetsGroup!, stop: UnsafePointer<ObjCBool>) in
println("is goin")
if group != nil
{
count++
var assetBlock : ALAssetsGroupEnumerationResultsBlock = {
(result: ALAsset!, index: Int, stop: UnsafePointer<ObjCBool>) in
if result != nil
{
imageArray.append(result.thumbnail().takeRetainedValue())
assetsArray.append(result)
countOne++
}
}
}
group.enumerateAssetsUsingBlock(assetBlock)
}
这篇关于SWIFT ALAssets库没有枚举组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!