SWIFT ALAssetsLibrary not enumerating groups

一曲冷凌霜 提交于 2019-12-12 15:34:49

问题


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)


}


}

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!


回答1:


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)


}



回答2:


I had the problem that the stubbed out code was returning 0 for the number of sections, so i changed it to 1 at least for the example i was working on: https://github.com/thegreatmichael/Albums-iOS8-Swift/blob/master/Albums/IAAlbumsViewController.swift#L66



来源:https://stackoverflow.com/questions/25191579/swift-alassetslibrary-not-enumerating-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!