iPhone - 如何创建自定义相册并以编程方式为相机胶卷中的照片指定自定义名称?

标签 iphone ios camera nsfilemanager

我正在开发一个 iPhone 照片应用程序,所以我需要在相机胶卷中创建一个名为“My Album”的单独相册,并且我需要使用自定义名称(例如“My Image.png”)保存我的 UIImageView 图像新创建的目录。

我该怎么做?

最佳答案

由于 AssetsLibrary 已弃用,请改用 Photos 框架(iOS 8 及更高版本)。

// Deprecated!
import AssetsLibrary

// Swift 3.0
let assetsLibrary = ALAssetsLibrary()
assetsLibrary.addAssetsGroupAlbum(withName: "NewAlbum", resultBlock: { assetsGroup in
    print(assetsGroup == nil ? "Already created" : "Success")
}, failureBlock: { error in
    print(error)
})

您可以使用共享的 PHPhotoLibrary 对象来创建新照片,但不能为它们指定特定名称,因为您将使用需要由Photos.app。每个 Assets 都有特定的属性。您可以获取对象、请求更改、 Assets /缩略图加载和缓存等。

要创建自定义相册,请使用 PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle:) .

简要示例:

// Swift 3.0
func createPhotoLibraryAlbum(name: String) {
    var albumPlaceholder: PHObjectPlaceholder?
    PHPhotoLibrary.shared().performChanges({
        // Request creating an album with parameter name
        let createAlbumRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name)
        // Get a placeholder for the new album
        albumPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection
    }, completionHandler: { success, error in
        if success {
            guard let placeholder = albumPlaceholder else {
                fatalError("Album placeholder is nil")
            }

            let fetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [placeholder.localIdentifier], options: nil)
            guard let album: PHAssetCollection = fetchResult.firstObject else {
                // FetchResult has no PHAssetCollection
                return
            }

            // Saved successfully!
            print(album.assetCollectionType)
        }
        else if let e = error {
            // Save album failed with error
        }
        else {
            // Save album failed with no error
        }
    })
}

不要忘记导入照片库。

要在该相册上创建新照片 Assets ,请使用 PHAssetChangeRequest.creationRequestForAsset(from:) .

// Swift 3.0
func createPhotoOnAlbum(photo: UIImage, album: PHAssetCollection) {
    PHPhotoLibrary.shared().performChanges({
        // Request creating an asset from the image
        let createAssetRequest = PHAssetChangeRequest.creationRequestForAsset(from: photo)
        // Request editing the album
        guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: album) else {
            // Album change request has failed
            return
        }
        // Get a placeholder for the new asset and add it to the album editing request
        guard let photoPlaceholder = createAssetRequest.placeholderForCreatedAsset else {
            // Photo Placeholder is nil
            return
        }
        albumChangeRequest.addAssets([photoPlaceholder] as NSArray)
    }, completionHandler: { success, error in
        if success {
            // Saved successfully!
        }
        else if let e = error {
            // Save photo failed with error
        }
        else {
            // Save photo failed with no error
        }
    })
}

更新:

我们需要请求访问权限才能使用照片库:

PHPhotoLibrary.requestAuthorization { status in
     switch status {
     ...
}

从 iOS 10 及更高版本开始,我们还需要在目标 .plist 文件中为“隐私 - 照片库使用说明”添加访问条目:

<key>NSPhotoLibraryUsageDescription</key>
<string>Access to photos is needed to provide app features</string>

关于iPhone - 如何创建自定义相册并以编程方式为相机胶卷中的照片指定自定义名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12152430/

相关文章:

iphone - 如何创建一个可以水平滚动的UITableView?

ios - 带有分组 TableView 的 UISearchBar——在 Swift 中

ios - 如何向 uiviewcontroller 中的导航栏添加后退按钮?

android - 带透明边距的相机

不同商店中的 iPhone 应用程序名称

ios - Apple 推送通知服务 - 多个设备 token 对同一设备有效

c++ - 在 OpenGL 中计算相机的法线?

Android将视频保存到文件

ios - 这段 Swift 代码在做什么? func somefunc(其中 : (_: NSLayoutConstraint) -> Bool)

ios - 无法将应用程序上传到 Apple Testflight - 需要成为管理员