SWIFT ALAssetsLibrary不枚举组
我试图收集所有用户的图像缩略图到一个数组,但是当我调用ALAssetsLibrary
的enumerateAssetsUsingBlock
方法似乎没有发生。
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()被调用时,编译器打印number of groups 0 number of total assets 0 fatal error: unexpectedly found nil while unwrapping an Optional value
因为好像ALAssetsLibrary
组没有被枚举。 (“正在布莱恩”没有被打印)。 任何想法发生了什么? 提前致谢!
我发现这个问题。 在这个“小组”里,结果不应该是零,否则它会迅速崩溃。 所以我们应该检查是否为零的情况
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) }
我遇到了这个问题,即代码段的数量已经返回0,所以我至less将其改为1,例如我正在处理的代码: https : //github.com/thegreatmichael/Albums-iOS8-Swift/斑点/主/专辑/ IAAlbumsViewController.swift#L66