我正在开发一个iOS应用程序,需要从库中导入一些图像,尤其是“人物”相册图像,我需要将它们显示到我的应用程序中,但是我无法访问此相册,我尝试下面的代码:
func FetchCustomAlbumPhotos()
{
let albumName = "People"
var assetCollection = PHAssetCollection()
var albumFound = Bool()
var photoAssets = PHFetchResult<AnyObject>()
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", 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 }
_ = 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("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: .aspectFit ,
options: options,
resultHandler: {
(image, info) -> Void in
print("Image \(String(describing: image))")
self.addImgToArray(uploadImage: image!)
//self.photo = image!
/* The image is now available to us */
//self.addImgToArray(self.photo)
print("enum for image, This is number 2")
})
}
}
}
如果我创建了一个专辑并将其命名为“ test”,则可以获取它,但是无法获取我需要的任何内置专辑,如“ People”。 有帮助吗?
答案 0 :(得分:0)
首先:在任何情况下,您都不要尝试按标题访问相册;我懒惰时会这样做,但是用户可以更改用户相册的标题,因此不可靠。您应该使用标识符。
第二:要访问内置的智能相册,请使用smartAlbum
类型并正确设置子类型,如@rmaddy正确建议。
第三:但这不适用于People专辑。您根本无法访问它。
我很确定无法访问“人物”相册是一个错误,如此处报道:
http://www.openradar.me/29797672
我当然认为可能是故意对您隐藏的;我不知道。但是,恐怕您将不得不没有该功能。