PHFetchResult
相簿內(nèi)的資源否灾,用了存放的PHAsset
結(jié)果中的內(nèi)容可以像使用NSArray
類的方法獲取內(nèi)容一樣來獲取PHFetchResult
中的內(nèi)容腺劣。一個(gè)PHFetchResult
對象中的內(nèi)容是動態(tài)加載的榛做,如果你需要一些內(nèi)容它才會去照片庫中去獲取對應(yīng)的內(nèi)容桐款,這可以在處理大量的結(jié)果的時(shí)候提供一個(gè)最佳的性能阳柔。
PHCachingImageManager
帶緩存的圖片管理對象
stopCachingImagesForAllAssets
: 方法
allowsCachingHighQualityImages
:方法
requestImage
:獲取縮略圖方法
PHImageRequestOptions
配置獲取請求參數(shù)
synchronous
: 同步獲取圖片没酣、多圖轻专、高清會太卡
deliveryMode
:請求的圖像質(zhì)量和交付優(yōu)先級
// 為了平衡圖像質(zhì)量和響
case opportunistic
// 只提供最高質(zhì)量的圖像忆矛,無論它需要多少時(shí)間加載
case highQualityFormat
// 最快速的得到一個(gè)圖像結(jié)果,可能會犧牲圖像質(zhì)量
case fastFormat
resizeMode
:請求圖像數(shù)據(jù)時(shí)將圖像與目標(biāo)大小如何適應(yīng)
// 不做任何調(diào)整
case none
// 最快速的調(diào)整圖像大小请垛,有可能比給定大小
case fast
// 保證與給定大小相等
case exact
代碼
self.requestOptions?.deliveryMode = PHImageRequestOptionsDeliveryMode.highQualityFormat
self.requestOptions?.resizeMode = PHImageRequestOptionsResizeMode.exact
PHAsset
mediaType
: 類型
// 未知
case unknown
// 圖片
case image
// 視頻
case video
// 音頻
case audio
獲取文件大小
PHAssetResource *resource = [[PHAssetResource assetResourcesForAsset:self.phasset] firstObject];
long long size = [[resourcevalueForKey:@"fileSize"] longLongValue];
let result :PHAssetResource = PHAssetResource.assetResources(for: asset).first!
let size = NSString(format: "%d", result.value(forKey: "fileSize") as! UInt64).floatValue
PHPhotoLibrary
requestAuthorization
: 申請權(quán)限
performChangesAndWait
:方法
圖片保存到指定的相冊中
do{
try PHPhotoLibrary.shared().performChangesAndWait({
// 安全校驗(yàn)
if createdAssetId.count > 0 {
// 獲取系統(tǒng)相冊文件
let createdAssets:PHFetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [createdAssetId], options: nil)
let request: PHAssetCollectionChangeRequest = PHAssetCollectionChangeRequest.init(for: collection as! PHAssetCollection)!
request.insertAssets(createdAssets, at: NSIndexSet.init(index: 0) as IndexSet)
}else {
print("報(bào)錯(cuò) url: \(createdAssetId)")
}
})
}catch{
print("保存指定相冊 error:\(error)")
}
} catch{
print("打印報(bào)錯(cuò)1: \(error)")
}
PHFetchOptions
PHAssetCollection
fetchAssetCollections
:方法
PHCollectionList
fetchTopLevelUserCollections
:方法
由于系統(tǒng)返回的相冊集名稱為英文催训,我們需要轉(zhuǎn)換為中文
private func titleOfAlbumForChinse(title:String?) -> String? {
if title == "Slo-mo" {
return "慢動作"
} else if title == "Recently Added" {
return "最近添加"
} else if title == "Favorites" {
return "個(gè)人收藏"
} else if title == "Recently Deleted" {
return "最近刪除"
} else if title == "Videos" {
return "視頻"
} else if title == "All Photos" {
return "所有照片"
} else if title == "Selfies" {
return "自拍"
} else if title == "Screenshots" {
return "屏幕快照"
} else if title == "Camera Roll" {
return "相機(jī)膠卷"
}
return title
}
生成自定義相冊
// 獲取App 名稱
let title: String = Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String
print("打印相冊名稱: \(title)")
// 相冊
var collection :PHCollection? = nil
//列出所有用戶創(chuàng)建的相冊
let userCollections = PHCollectionList.fetchTopLevelUserCollections(with: nil)
let count:Int = userCollections.count
for i in 0..<count {
let c = userCollections[i]
let cName: String = c.localizedTitle!
print("打印 用戶創(chuàng)建的相冊:\(cName) title:\(title)")
if cName == title {
collection = c
break;
}
}
// 生成app相冊
if collection == nil {
// 相冊唯一識別ID
var createdCollectionID: String?
do {
try PHPhotoLibrary.shared().performChangesAndWait {
//創(chuàng)建一個(gè)相冊,拿到相冊的唯一標(biāo)識符
createdCollectionID = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: title).placeholderForCreatedAssetCollection.localIdentifier
}
}catch{
print("creationRequest.error: \(error)")
}
// 獲取相冊
collection = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [createdCollectionID!], options: nil).firstObject
}
return collection!
優(yōu)化卡頓問題
UICollectionView
- 盡可能格子不要出現(xiàn)太多,格子不要出現(xiàn)太大
- 修改控件屬性
isOpaque
= true宗收、masksToBounds
= true漫拭、shouldRasterize
= true 、contentsGravity
混稽、contentsScale
- PHCachingImageManager 的屬性:
allowsCachingHighQualityImages
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
self.imageManager.allowsCachingHighQualityImages = false
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if decelerate == false {
self.imageManager.allowsCachingHighQualityImages = true
}
}