beri Photos
kullanın lütfen bunun yerine çerçeve (iOS 8 ve üstü).
// 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)
})
Yeni fotoğraf oluşturmak için paylaşılan PHPhotoLibrary
nesneyi kullanabilirsiniz ancak Photos.app tarafından yönetilmesi gerekir varlıkları ile çalışıyor olacak, çünkü bunları belirli isimler veremez. Her varlığın belirli özellikleri vardır.
Nesneleri getirebilir, değişiklikler yapabilir, öğe/küçük resim yükleme ve önbelleğe alma, özel albümü oluşturmak için lütfen PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle:
) kullanın.
Kısa örnek:
// 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
}
})
}
import Photos
kütüphaneye unutmayın.
Bu albümde bir yeni fotoğraf öğesi oluşturmak için lütfen PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle:
)'u kullanın.
// 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
}
})
}
GÜNCELLEME: Biz Fotoğraflar kütüphanesini kullanmak edebilmek için erişim istemesi gerekir
: iOS 10 itibariyle yukarıda
PHPhotoLibrary.requestAuthorization { status in
switch status {
...
}
biz de giriş eklemek gerekir "Gizlilik - Fotoğraf Kitaplığı Kullanım Açıklaması" için hedef .plist dosyasına erişim için:
<key>NSPhotoLibraryUsageDescription</key>
<string>Access to photos is needed to provide app features</string>
Merhaba .. resimlere özel ad vermeyi öğrendiniz mi? –