Ponieważ AssetsLibrary
jest przestarzałe, użyj Photos
framework (system iOS 8 i nowsze wersje).
// 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)
})
Można użyć udostępnionego PHPhotoLibrary
obiekt do tworzenia nowych zdjęć, ale nie można dać im konkretne nazwy, ponieważ będziesz pracował z aktywów, które muszą być zarządzane przez Photos.app. Każdy zasób ma określone właściwości. Możesz pobierać obiekty, żądać zmian, ładować zasoby/miniaturki i buforować itd.
Aby utworzyć niestandardowy album, użyj numeru PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle:
).
Krótki przykład:
// 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
}
})
}
Nie zapomnij import Photos
bibliotece.
Aby utworzyć nowy zasób zdjęć na tym albumie, należy użyć numeru PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle:
).
// 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
}
})
}
UPDATE:
Musimy żądać dostępu, aby móc korzystać z biblioteki zdjęć:
PHPhotoLibrary.requestAuthorization { status in
switch status {
...
}
Jako iOS 10 a przede musimy także dodać wpis w celu uzyskania dostępu do docelowego pliku .plist dla "Prywatność - opis użycia biblioteki zdjęć":
<key>NSPhotoLibraryUsageDescription</key>
<string>Access to photos is needed to provide app features</string>
cześć .. Czy wiesz, jak nadać obrazom własną nazwę? –