将 PHAsset 附加到 PHAsset 数组
Append PHAsset to an array of 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)
我正在尝试获取相册中的所有照片 (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)