ios - 将 PHAsset 附加到 PHAsset 数组

标签 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)
    }

最佳答案

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

var arrayOfPHAsset : [PHAsset]!

您还需要分配数组。

var arrayOfPHAsset : [PHAsset] = []

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

arrayOfPHAsset.append(asset)

关于ios - 将 PHAsset 附加到 PHAsset 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45974363/

相关文章:

ios - 使用 Swift 4 中的 PHAssetCollection 从特定相册中获取特定照片

android - Android和IOS上的SDK检测

ios - 我应该使用 UIDocument 来处理已保存到磁盘的 PDF 文件吗?

ios - 正确定义 UITableViewCell 调整大小的约束

ios - 使用关联类型创建元组

swift - ios13 使用 PHImageManager 获取原始图像

ios - 游戏中心和我自己的服务器

ios - 如何将数据从 View Controller 传递到容器?

ios - 如何从 App Delegate (XCode 9) 访问 View Controller

ios - 没有获取图像路径