前言
很多时候直接调用系统的选择照片是无法满足我们的需求的,所以我们需要自定义选择相册,如图1和图2的效果
图1 图2代码
为了完成这样的效果,我稍微的封装了一下Photos框架的一些方法,方便我调用
管理类:
import UIKit
import Photos
class AlbumManager: NSObject {
static let shared = AlbumManager()
private override init() {}
// 判断有无相册权限
func hasAuth() -> Bool {
let status = PHPhotoLibrary.authorizationStatus()
if status == PHAuthorizationStatus.restricted || status == PHAuthorizationStatus.denied {
return true
} else {
return false
}
}
// 获得所有相册
func allAlbums(complete: @escaping (_ array: Array<AlbumModel>) -> Void) {
var dataArray: Array<AlbumModel> = []
let albums = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum, subtype: PHAssetCollectionSubtype.any, options: nil)
albums.enumerateObjects { (collection, index, pointer) in
if collection.isKind(of: PHAssetCollection.self) {
let fetchResult = PHAsset.fetchAssets(in: collection, options: nil)
if fetchResult.count > 0 {
let asset = fetchResult.lastObject
let opt = PHImageRequestOptions.init()
opt.isSynchronous = true
let manager = PHImageManager.init()
manager.requestImageData(for: asset!, options: opt, resultHandler: { (data, dataUTI, orientation, info) in
let result = UIImage.init(data: data!)
let compressionData = result?.jpegData(compressionQuality: 0)
let model = AlbumModel.init()
model.albumName = collection.localizedTitle
model.firstImage = UIImage.init(data: compressionData!)
model.collection = collection
model.fetchResult = fetchResult
dataArray.append(model)
})
}
}
}
complete(dataArray)
}
// 获得一个相册的所有图片
func getImages(fetch: PHFetchResult<PHAsset>, size: CGSize, complete: @escaping (_ array: Array<UIImage>) -> Void) {
var dataArray: Array<UIImage> = []
autoreleasepool {
fetch.enumerateObjects(options: NSEnumerationOptions.reverse, using: { (asset, index, pointer) in
let opt = PHImageRequestOptions.init()
opt.isSynchronous = true
let manager = PHImageManager.init()
manager.requestImage(for: asset, targetSize: size, contentMode: PHImageContentMode.aspectFill, options: opt, resultHandler: { (image, info) in
dataArray.append(image!)
})
})
}
complete(dataArray)
}
// 获得原图
func getOriginalImage(asset: PHAsset, complete: @escaping (_ image: UIImage) -> Void) {
let opt = PHImageRequestOptions.init()
opt.isSynchronous = true
let manager = PHImageManager.init()
manager.requestImage(for: asset, targetSize: CGSize.init(width: asset.pixelWidth, height: asset.pixelHeight), contentMode: PHImageContentMode.aspectFill, options: opt) { (image, info) in
complete(image!)
}
}
// 获得图片
func getImage(asset: PHAsset, size: CGSize, complete: @escaping (_ image: UIImage) -> Void) {
let opt = PHImageRequestOptions.init()
opt.isSynchronous = true
let manager = PHImageManager.init()
manager.requestImage(for: asset, targetSize: size, contentMode: PHImageContentMode.aspectFill, options: opt) { (image, info) in
complete(image!)
}
}
func save(image: UIImage) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(save(image:didFinishWithError:contentInfo:)), nil)
}
}
模型
class AlbumModel {
var albumName: String?
var firstImage: UIImage?
var collection: PHAssetCollection?
var fetchResult: PHFetchResult<PHAsset>?
}
需要注意的
- PHAssetCollectionType是相册的类型,我这里选的是智能相册,实际上里面还有别的,可以根据需要选择
@available(iOS 8.0, *)
public enum PHAssetCollectionType : Int {
case album
case smartAlbum
case moment
}
- 可以发现我加了autoreleasepool,是因为我发现遍历内存完竟然没有及时释放,这里的原因我不太了解,加了以后效果好多了。
网友评论