gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:06:19 30 4
gpt4 key购买 nike

我正在开发一个 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/44993007/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com