Ittai Svidler
Ittai Svidler

Reputation: 31

SWIFT ALAssetsLibrary not enumerating groups

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!

Upvotes: 3

Views: 5198

Answers (2)

Michael
Michael

Reputation: 2993

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

Upvotes: 0

shahrukh jain
shahrukh jain

Reputation: 21

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)


}

Upvotes: 2

Related Questions