我尝试过以下方法,但不保存带有指定名称的图像。
首次尝试:成功,但未以指定名称保存
var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
var dir = paths[0] as! String
var filePath = "\\(dir)/imageName.jpg"
var saveImage = UIImage(contentsOfFile: filePath)
UIImageWriteToSavedPhotosAlbum(saveImage, self, nil, nil)
第二次尝试:无法保存图片
var data = UIImageJPEGRepresentation(image, 1.0)
data.writeToFile(filePath, true)
最后我将我的图像保存到具有特定名称的NSSearchPathDirectory.DocumentDirectory。即使它不可见,我们也可以搜索具有特定名称的文件,并在UIImageView中打开/显示。
据我所知,iPhone不允许您通过名称Image来编写或搜索照片库,以编程方式。如果可以,请告诉我如何。
答案 0 :(得分:2)
在你使用它之前,我建议你请浏览iOS的照片框架。
为同一个创建单例类 -
class CreateCustomPhotoAlbum: NSObject
{
static let albumName = "Your album name"
static let sharedInstance = createCustomPhotoAlbum()
var assetCollection: PHAssetCollection!
override init() {
super.init()
if let assetCollection = fetchAssetCollectionForAlbum()
{
self.assetCollection = assetCollection
return
}
if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.Authorized
{
PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) -> Void in
status
})
}
if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.Authorized {
self.createAlbum()
} else {
PHPhotoLibrary.requestAuthorization(requestAuthorizationHandler)
}
}
func requestAuthorizationHandler(status: PHAuthorizationStatus)
{
if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.Authorized {
// ideally this ensures the creation of the photo album even if authorization wasn't prompted till after init was done
print("trying again to create the album")
self.createAlbum()
} else {
print("should really prompt the user to let them know it's failed")
}
}
func createAlbum() {
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(createCustomPhotoAlbum.albumName) // create an asset collection with the album name
}) { success, error in
if success {
self.assetCollection = self.fetchAssetCollectionForAlbum()
} else {
print("error \(error)")
}
}
}
func fetchAssetCollectionForAlbum() -> PHAssetCollection!
{
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", createCustomPhotoAlbum.albumName)
let collection = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
if let _: AnyObject = collection.firstObject {
return collection.firstObject as! PHAssetCollection
}
return nil
}
func saveImage(image: UIImage)
{
if assetCollection == nil
{
return // if there was an error upstream, skip the save
}
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection)
albumChangeRequest!.addAssets([assetPlaceHolder!])
}, completionHandler: nil)
}
}
保存图片
只需简单地调用它 -
createCustomPhotoAlbum.sharedInstance.saveImage(imageName!)