我有一个自定义相册,其中我的应用程序从相机保存图片。我想知道是否有一种方法可以在我的相册中创建文件夹,以便我可以在其中堆叠某些图像?
ANSWER正如其他成员提到的,可悲的是,你无法在相册中创建一个文件夹。
您应该尝试下面的代码。它是Swift 3.0syntaxe。:)
import Foundation
import Ptos
cl CustomPtoAl: NSObject {
static let alName = "Al Name"
static let sharedInstance = CustomPtoAl()
var etCollection: PHAssetCollection!
override init() {
super.init()
if let etCollection = fetchAssetCollectionForAl() {
self.etCollection = etCollection
return
}
if PHPtoLibrary.autrizationStatus() != PHAutrizationStatus.autrized {
PHPtoLibrary.requestAutrization({ (status: PHAutrizationStatus) -> Void in
()
})
}
if PHPtoLibrary.autrizationStatus() == PHAutrizationStatus.autrized {
self.createAl()
} else {
PHPtoLibrary.requestAutrization(requestAutrizationHandler)
}
}
func requestAutrizationHandler(status: PHAutrizationStatus) {
if PHPtoLibrary.autrizationStatus() == PHAutrizationStatus.autrized {
// ideally this ensures the creation of the pto al even if autrization wasn't prompted till after init was done
print("trying again to create the al")
self.createAl()
} else {
print("suld really prompt the user to let them know it's failed")
}
}
func createAl() {
PHPtoLibrary.shared().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: CustomPtoAl.alName) // create an et collection with the al name
}) { success, error in
if success {
self.etCollection = self.fetchAssetCollectionForAl()
} else {
print("error \(error)")
}
}
}
func fetchAssetCollectionForAl() -> PHAssetCollection? {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "le = %@", CustomPtoAl.alName)
let collection = PHAssetCollection.fetchAssetCollections(with: .al, subtype: .any, options: fetchOptions)
if let _: AnyObject = collection.firstObject {
return collection.firstObject
}
return nil
}
func save(image: UIImage) {
if etCollection == nil {
return // if there was an error upstream, skip the save
}
PHPtoLibrary.shared().performChanges({
let etChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
let etPlaceHolder = etChangeRequest.placelderForCreatedAsset
let alChangeRequest = PHAssetCollectionChangeRequest(for: self.etCollection)
let enumeration: NSArray = [etPlaceHolder!]
alChangeRequest!.addAssets(enumeration)
}, completionHandler: nil)
}
}
您可以在用户的相册中的文件夹中添加相册。
import Ptos
cl PtoManager {
static let instance = PtoManager()
var folder: PHCollectionList?
/// Fetches an existing folder with the specified identifier or creates one with the specified name
func fetchFolderWithIdentifier(_ identifier: String, name: String) {
let fetchResult = PHCollectionList.fetchCollectionLists(withLocalIdentifiers: [identifier], options: nil)
guard let folder = fetchResult.firstObject else {
createFolderWithName(name)
return
}
self.folder = folder
}
/// Creates a folder with the specified name
private func createFolderWithName(_ name: String) {
var placelder: PHObjectPlacelder?
PHPtoLibrary.shared().performChanges({
let changeRequest = PHCollectionListChangeRequest.creationRequestForCollectionList(withTitle: name)
placelder = changeRequest.placelderForCreatedCollectionList
}) { (success, error) in
guard let placelder = placelder else { return }
let fetchResult = PHCollectionList.fetchCollectionLists(withLocalIdentifiers: [placelder.localIdentifier], options: nil)
guard let folder = fetchResult.firstObject else { return }
self.folder = folder
}
}
/// Creates an al with the specified name
private func createAlWithName(_ name: String, completion: @escaping (PHAssetCollection?) -> Void) {
guard let folder = folder else {
completion(nil)
return
}
var placelder: PHObjectPlacelder?
PHPtoLibrary.shared().performChanges({
let listRequest = PHCollectionListChangeRequest(for: folder)
let createAlRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name)
listRequest?.addChildCollections([createAlRequest.placelderForCreatedAssetCollection] as NSArray)
placelder = createAlRequest.placelderForCreatedAssetCollection
}) { (success, error) in
guard let placelder = placelder else {
completion(nil)
return
}
let fetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [placelder.localIdentifier], options: nil)
let al = fetchResult.firstObject
completion(al)
}
}
/// Saves the image to a new al with the specified name
func saveImageToAlInRootFolder(_ alName: String, image: UIImage?, completion: @escaping (Error?) -> Void) {
createAlWithName(alName) { (al) in
guard let al = al else {
return
}
PHPtoLibrary.shared().performChanges({
let alChangeRequest = PHAssetCollectionChangeRequest(for: al)
let createAssetRequest = PHAssetChangeRequest.creationRequestForAsset(from: image!)
let ptoPlacelder = createAssetRequest.placelderForCreatedAsset!
alChangeRequest?.addAssets([ptoPlacelder] as NSArray)
}, completionHandler: { (success, error) in
if success {
completion(nil)
} else if let error = error {
// Failed with error
} else {
// Failed with no error
}
})
}
}
}
这允许你做这样的事情:
let defaults = UserDefaults.standard
let identifier = defaults.string(forKey: "myFolder")!
PtoManager.instance.fetchFolderWithIdentifier(identifier, name: "My Folder")
PtoManager.instance.saveImageToAlInRootFolder("My Al", image: UIImage(named: "my_image")) { (error) in
// Handle error
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(85条)