如何在通过iOS swift photos framework保存照片时命名照片
我在swift中使用了如何在通过iOS swift photos framework保存照片时命名照片,ios,swift,xcode,frameworks,photo,Ios,Swift,Xcode,Frameworks,Photo,我在swift中使用了Xcode 6.3(我认为是1.2版)。我的应用程序从web上的自定义服务器下载照片。然后,我希望它将具有给定文件名的照片保存到自定义相册中,该相册是通过照片框架创建的。我需要能够控制照片/文件名,因为我会跟踪我在数据库中下载的照片,然后需要按名称查看它们 我创建了一个自定义的PhotoAlbum类,试图将与相册交互的所有逻辑封装在其中。下载照片后,我可以调用以下代码,它可以创建相册并保存照片,但正如您在savePhoto()方法中注意到的那样,我不知道在保存时在何处/如何
Xcode 6.3
(我认为是1.2版)。我的应用程序从web上的自定义服务器下载照片。然后,我希望它将具有给定文件名的照片保存到自定义相册中,该相册是通过照片框架创建的。我需要能够控制照片/文件名,因为我会跟踪我在数据库中下载的照片,然后需要按名称查看它们
我创建了一个自定义的PhotoAlbum类,试图将与相册交互的所有逻辑封装在其中。下载照片后,我可以调用以下代码,它可以创建相册并保存照片,但正如您在savePhoto()
方法中注意到的那样,我不知道在保存时在何处/如何使用photoName变量来命名照片。最后,我还需要知道如何在loadPhoto()
方法中按名称检索照片
谢谢你能给我的任何帮助/建议
let myAlbum = PhotoAlbum("MyAlbum")
myAlbum.savePhoto(downloadedPhoto, 12)
class PhotoAlbum {
var albumName: String = ""
var albumExists: Bool = false
var assetCollection: PHAssetCollection!
var photosAsset: PHFetchResult!
init(albumName: String) {
self.albumName = albumName
self.initializeAlbum()
}
private func initializeAlbum() {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", self.albumName)
let collection: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
if let firstObject: AnyObject = collection.firstObject {
self.albumExists = true
self.assetCollection = collection.firstObject as! PHAssetCollection
if let ac = self.assetCollection {
self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
}
println("\(self.albumName) album exists")
}
else {
var albumPlaceHolder: PHObjectPlaceholder!
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(self.albumName)
albumPlaceHolder = request.placeholderForCreatedAssetCollection
}, completionHandler: { (success: Bool, error: NSError!) in
self.albumExists = success
if success {
let collection = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([albumPlaceHolder.localIdentifier], options: nil)
self.assetCollection = collection.firstObject as! PHAssetCollection
if let ac = self.assetCollection {
self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
}
println("\(self.albumName) album made")
}
else {
println("Failed to create \(self.albumName) Album!!!!!!")
}
})
}
}
func savePhoto(image: UIImage, photoID: Int) {
let photoName = String(photoID)+".jpg"
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
let assetPlaceholder = assetRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset)
albumChangeRequest.addAssets([assetPlaceholder])
}, completionHandler: { (success: Bool, error: NSError!) in
print(success ? "image added" : "failed to add image")
})
}
func loadPhoto(photoID: Int64) -> UIImage {
let photoName = String(photoID)+".jpg"
return UIImage(named: "no_photo")!
}
func renameAlbum(newAlbumName: String) {
// need to create the new album and move all photos to that album then remove the old album
}
func reloadPhotos() {
if let ac = self.assetCollection {
self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
}
}
您需要记录正在创建的资产的
localIdentifier
属性,而不是“命名”文件localIdentifier
是一个持久标识符(即,在重启/重新加载期间,它将是资产的一致ID)
我已经拼凑了一些基本代码,您可以使用这些代码在保存图像时获取localIdentifier
,并通过localIdentifier
检索图像。希望这能让你走
import UIKit
import Photos
class MRPhotosHelper {
var manager = PHImageManager.defaultManager()
func saveImageAsAsset(image: UIImage, completion: (localIdentifier:String?) -> Void) {
var imageIdentifier: String?
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
let changeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
let placeHolder = changeRequest.placeholderForCreatedAsset
imageIdentifier = placeHolder.localIdentifier
}, completionHandler: { (success, error) -> Void in
if success {
completion(localIdentifier: imageIdentifier)
} else {
completion(localIdentifier: nil)
}
})
}
func retrieveImageWithIdentifer(localIdentifier:String, completion: (image:UIImage?) -> Void) {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "mediaType == %d", PHAssetMediaType.Image.rawValue)
let fetchResults = PHAsset.fetchAssetsWithLocalIdentifiers([localIdentifier], options: fetchOptions)
if fetchResults.count > 0 {
if let imageAsset = fetchResults.objectAtIndex(0) as? PHAsset {
let requestOptions = PHImageRequestOptions()
requestOptions.deliveryMode = .HighQualityFormat
manager.requestImageForAsset(imageAsset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFill, options: requestOptions, resultHandler: { (image, info) -> Void in
completion(image: image)
})
} else {
completion(image: nil)
}
} else {
completion(image: nil)
}
}
}
然后,您可以这样做:
// get a reference to our helper
let helper = MRPhotosHelper()
// a var to store the localIdentifier for the image we save
var identifier:String?
// save the image to library
if let imageToSave = UIImage(named: "SomeImageName"){
helper.saveImageAsAsset(imageToSave, completion: { (localIdentifier) -> Void in
identifier = localIdentifier
})
}
// other stuff
// retrieve image from library at later date
if let identifier = identifier {
helper.retrieveImageWithIdentifer(identifier, completion: { (image) -> Void in
let retrievedImage = image
})
}
在使用更新UI之前,您可能需要检查您是否回到完成闭包中的主线程。此外,如果我无法“命名”照片,那么是否有任何类型的标识符绑定到该照片/资产,以便我可以保存,以便以后可以通过该标识符请求该照片?