小米相册在哪个文件夹:在自定义相册中创建文件夹(how to create new folder on iphone)

我有一个自定义相册,其中我的应用程序从相机保存图片。我想知道是否有一种方法可以在我的相册中创建文件夹,以便我可以在其中堆叠某些图像?

ANSWER

正如其他成员提到的,可悲的是,你无法在相册中创建一个文件夹。

7

您应该尝试的代码。它是Swift 3.0syntaxe。:)

import Foundation
import Photos
class CustomPhotoAl: NSObject {
    static let alName = "Al Name"
    static let sharedInstance = CustomPhotoAl()
    var assetCollection: PHAssetCollection!
    override init() {
        super.init()
        if let assetCollection = fetchAssetCollectionForAl() {
            self.assetCollection = assetCollection
            return
        }
        if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.authorized {
            PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) -> Void in
                ()
            })
        }
        if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized {
            self.createAl()
        } else {
            PHPhotoLibrary.requestAuthorization(requestAuthorizationHandler)
        }
    }
    func requestAuthorizationHandler(status: PHAuthorizationStatus) {
        if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized {
            // ideally this ensures the creation of the photo al even if authorization wasn't prompted till after init was done
            print("trying again to create the al")
            self.createAl()
        } else {
            print("should really prompt the user to let them know it's failed")
        }
    }
    func createAl() {
        PHPhotoLibrary.shared().performChanges({
            PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: CustomPhotoAl.alName)   // create an asset collection with the al name
        }) { success, error in
            if success {
                self.assetCollection = self.fetchAssetCollectionForAl()
            } else {
                print("error \(error)")
            }
        }
    }
    func fetchAssetCollectionForAl() -> PHAssetCollection? {
        let fetchOptions = PHFetchOptions()
        fetchOptions.predicate = NSPredicate(format: "title = %@", CustomPhotoAl.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 assetCollection == nil {
            return                          // if there was an error upstream, skip the save
        }
        PHPhotoLibrary.shared().performChanges({
            let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
            let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
            let alChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
            let enumeration: NSArray = [assetPlaceHolder!]
            alChangeRequest!.addAssets(enumeration)
        }, completionHandler: nil)
    }
}
7

您可以在用户的相册中的文件夹中添加相册。

import Photos
class PhotoManager {
    static let instance = PhotoManager()
    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 placeholder: PHObjectPlaceholder?
        PHPhotoLibrary.shared().performChanges({
            let changeRequest = PHCollectionListChangeRequest.creationRequestForCollectionList(withTitle: name)
            placeholder = changeRequest.placeholderForCreatedCollectionList
        }) { (success, error) in
            guard let placeholder = placeholder else { return }
            let fetchResult = PHCollectionList.fetchCollectionLists(withLocalIdentifiers: [placeholder.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 placeholder: PHObjectPlaceholder?
        PHPhotoLibrary.shared().performChanges({
            let listRequest = PHCollectionListChangeRequest(for: folder)
            let createAlRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name)
            listRequest?.addChildCollections([createAlRequest.placeholderForCreatedAssetCollection] as NSArray)
            placeholder = createAlRequest.placeholderForCreatedAssetCollection
        }) { (success, error) in
            guard let placeholder = placeholder else {
                completion(nil)
                return
            }
            let fetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [placeholder.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
            }
            PHPhotoLibrary.shared().performChanges({
                let alChangeRequest = PHAssetCollectionChangeRequest(for: al)
                let createAssetRequest = PHAssetChangeRequest.creationRequestForAsset(from: image!)
                let photoPlaceholder = createAssetRequest.placeholderForCreatedAsset!
                alChangeRequest?.addAssets([photoPlaceholder] 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")!
PhotoManager.instance.fetchFolderWithIdentifier(identifier, name: "My Folder")
PhotoManager.instance.saveImageToAlInRootFolder("My Al", image: UIImage(named: "my_image")) { (error) in
     // Handle error
}

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(952)
Linux查看cpu实时频率的命令:linux:不能用cpupower设置cpu频率调节器 (ACPI故障)
上一篇
Men ev it:Cómocentroestemenúdesplegable(下拉菜单)
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(32条)