将PHAsset附加到PHAsset数组

时间:2017-08-31 06:07:52

标签: ios swift phasset

我正在尝试获取相册中的所有照片(PHAssets)并将它们附加到数组中,以便将其作为完成参数传递。

代码在包含arrayOfPHAsset.append(asset)的行崩溃。为什么?我需要在代码中进行哪些更改才能使其正常工作?

这是我正在使用的代码。

func fetchCustomAlbumPhotos( completion : (_ array : [PHAsset]) -> Void)
    {
        var assetCollection = PHAssetCollection()
        var albumFound = Bool()
        var photoAssets = PHFetchResult<AnyObject>()
        var arrayOfPHAsset : [PHAsset]!
        let fetchOptions = PHFetchOptions()
        fetchOptions.predicate = NSPredicate(format: "title = %@", CustomAlbum.albumName)
        let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)

        if let _:AnyObject = collection.firstObject{
            //found the album
            assetCollection = collection.firstObject!
            albumFound = true
        }else {
            albumFound = false
        completion([])
        }
        _ = collection.count
        photoAssets = PHAsset.fetchAssets(in: assetCollection, options: nil) as! PHFetchResult<AnyObject>
        let imageManager = PHCachingImageManager()

        //        let imageManager = PHImageManager.defaultManager()

        photoAssets.enumerateObjects({(object: AnyObject!,
            count: Int,
            stop: UnsafeMutablePointer<ObjCBool>) in

            if object is PHAsset{
                let asset = object as! PHAsset
                print(asset)
                arrayOfPHAsset.append(asset)
//                print("Inside  If object is PHAsset, This is number 1")
//                
//                let imageSize = CGSize(width: asset.pixelWidth,
//                                       height: asset.pixelHeight)
//                
//                /* For faster performance, and maybe degraded image */
//                let options = PHImageRequestOptions()
//                options.deliveryMode = .fastFormat
//                options.isSynchronous = true
//                
//                imageManager.requestImage(for: asset,
//                                                  targetSize: imageSize,
//                                                  contentMode: .aspectFill,
//                                                  options: options,
//                                                  resultHandler: {
//                                                    (image, info) -> Void in
////                                                    self.photo = image!
////                                                    /* The image is now available to us */
////                                                    self.addImgToArray(uploadImage: self.photo)
//                                                    print("enum for image, This is number 2")
//                                                    
//                })

            }
        })
        print("arrayOfPHAsset : \(arrayOfPHAsset), arrayOfPHAsset count : \(arrayOfPHAsset.count)")
        completion(arrayOfPHAsset)
    }

1 个答案:

答案 0 :(得分:3)

您要显示的代码中的以下行未分配数组;它只是在声明它。

var arrayOfPHAsset : [PHAsset]!

您也需要分配数组。

var arrayOfPHAsset : [PHAsset] = []

然后您可以将项目添加到数组中。

arrayOfPHAsset.append(asset)