用phimagemanager保存图像到自定义相册?
我正在制作一个应用程序,用AVFoundation拍照,我想将它们保存到自定义相册,然后我可以查询和显示在我的应用程序。 (除非用户需要,否则我不希望在普通照片中包含这些内容)我无法在Swift中find任何显示如何执行此操作的内容。 有没有不同的方式,我应该这样做?
我在SO上find了这个例子,但对我来说没有任何意义,我也无法使它起作用。
func savePhoto() { var albumFound : Bool = false var assetCollection: PHAssetCollection! var photosAsset: PHFetchResult! var assetThumbnailSize:CGSize! // Create the album if does not exist (in viewDidLoad) if let first_Obj:AnyObject = collection.firstObject{ //found the album self.albumFound = true self.assetCollection = collection.firstObject as PHAssetCollection }else{ //Album placeholder for the asset collection, used to reference collection in completion handler var albumPlaceholder:PHObjectPlaceholder! //create the folder NSLog("\nFolder \"%@\" does not exist\nCreating now...", albumName) PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(albumName) albumPlaceholder = request.placeholderForCreatedAssetCollection }, completionHandler: {(success:Bool, error:NSError!)in NSLog("Creation of folder -> %@", (success ? "Success":"Error!")) self.albumFound = (success ? true:false) if(success){ let collection = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([albumPlaceholder.localIdentifier], options: nil) self.assetCollection = collection?.firstObject as PHAssetCollection } }) } let bundle = NSBundle.mainBundle() let myFilePath = bundle.pathForResource("highlight1", ofType: "mov") let videoURL:NSURL = NSURL.fileURLWithPath(myFilePath!)! let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT dispatch_async(dispatch_get_global_queue(priority, 0), { PHPhotoLibrary.sharedPhotoLibrary().performChanges({ //let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(videoURL) let assetPlaceholder = createAssetRequest.placeholderForCreatedAsset let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset) albumChangeRequest.addAssets([assetPlaceholder]) }, completionHandler: {(success, error)in dispatch_async(dispatch_get_main_queue(), { NSLog("Adding Image to Library -> %@", (success ? "Sucess":"Error!")) //picker.dismissViewControllerAnimated(true, completion: nil) }) }) }) }
任何帮助/解释将是伟大的!
我是这样做的:
在顶部:
import Photos var image: UIImage! var assetCollection: PHAssetCollection! var albumFound : Bool = false var photosAsset: PHFetchResult! var assetThumbnailSize:CGSize! var collection: PHAssetCollection! var assetCollectionPlaceholder: PHObjectPlaceholder!
创build相册:
func createAlbum() { //Get PHFetch Options let fetchOptions = PHFetchOptions() fetchOptions.predicate = NSPredicate(format: "title = %@", "camcam") let collection : PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions) //Check return value - If found, then get the first album out if let _: AnyObject = collection.firstObject { self.albumFound = true assetCollection = collection.firstObject as! PHAssetCollection } else { //If not found - Then create a new album PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let createAlbumRequest : PHAssetCollectionChangeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle("camcam") self.assetCollectionPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection }, completionHandler: { success, error in self.albumFound = success if (success) { let collectionFetchResult = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([self.assetCollectionPlaceholder.localIdentifier], options: nil) print(collectionFetchResult) self.assetCollection = collectionFetchResult.firstObject as! PHAssetCollection } }) } }
保存照片时:
func saveImage(){ PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(self.image) let assetPlaceholder = assetRequest.placeholderForCreatedAsset let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset) albumChangeRequest!.addAssets([assetPlaceholder!]) }, completionHandler: { success, error in print("added image to album") print(error) self.showImages() }) }
显示该相册中的图片:
func showImages() { //This will fetch all the assets in the collection let assets : PHFetchResult = PHAsset.fetchAssetsInAssetCollection(assetCollection, options: nil) print(assets) let imageManager = PHCachingImageManager() //Enumerating objects to get a chached image - This is to save loading time assets.enumerateObjectsUsingBlock{(object: AnyObject!, count: Int, stop: UnsafeMutablePointer<ObjCBool>) in if object is PHAsset { let asset = object as! PHAsset print(asset) let imageSize = CGSize(width: asset.pixelWidth, height: asset.pixelHeight) let options = PHImageRequestOptions() options.deliveryMode = .FastFormat imageManager.requestImageForAsset(asset, targetSize: imageSize, contentMode: .AspectFill, options: options, resultHandler: {(image: UIImage?, info: [NSObject : AnyObject]?) in print(info) print(image) }) } }
回答Objective-C并清理一下。
__block PHFetchResult *photosAsset; __block PHAssetCollection *collection; __block PHObjectPlaceholder *placeholder; // Find the album PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", @"YOUR_ALBUM_TITLE"]; collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions].firstObject; // Create the album if (!collection) { [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetCollectionChangeRequest *createAlbum = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"YOUR_ALBUM_TITLE"]; placeholder = [createAlbum placeholderForCreatedAssetCollection]; } completionHandler:^(BOOL success, NSError *error) { if (success) { PHFetchResult *collectionFetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[placeholder.localIdentifier] options:nil]; collection = collectionFetchResult.firstObject; } }]; } // Save to the album [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[UIImage imageWithData:imageData]]; placeholder = [assetRequest placeholderForCreatedAsset]; photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:nil]; PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection assets:photosAsset]; [albumChangeRequest addAssets:@[placeholder]]; } completionHandler:^(BOOL success, NSError *error) { if (success) { NSString *UUID = [placeholder.localIdentifier substringToIndex:36]; self.photo.assetURL = [NSString stringWithFormat:@"assets-library://asset/asset.PNG?id=%@&ext=JPG", UUID]; [self savePhoto]; } else { NSLog(@"%@", error); } }];
在UUID结尾的位是我在另一个StackOverflow线程上find的,用于从ALAsset创buildAssetURL属性的replace。
注意:请参阅下面的克里斯'评论更完整的答案。
我喜欢重复使用我编写的代码,所以我决定创build一个PHPhotoLibrary的扩展,可以像这样使用它:
PHPhotoLibrary.saveImage(photo, albumName: "Trip") { asset in guard let asset = asset else { assert(false, "Asset is nil") return } PHPhotoLibrary.loadThumbnailFromAsset(asset) { thumbnail in print(thumbnail) } }
这里是代码: https : //gist.github.com/ricardopereira/636ccd0a3c8a327c43d42e7cbca4d041
更新为Swift 2.1+和video,如果你正在尝试这样做,并在这里结束。 比较其他答案的细微差异(如使用图像,而不是video)
var photosAsset: PHFetchResult! var collection: PHAssetCollection! var assetCollectionPlaceholder: PHObjectPlaceholder! //Make sure we have custom album for this app if haven't already let fetchOptions = PHFetchOptions() fetchOptions.predicate = NSPredicate(format: "title = %@", "MY_APP_ALBUM_NAME") self.collection = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions).firstObject as! PHAssetCollection //if we don't have a special album for this app yet then make one if self.collection == nil { PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let createAlbumRequest : PHAssetCollectionChangeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle("MY_APP_ALBUM_NAME") self.assetCollectionPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection }, completionHandler: { success, error in if success { let collectionFetchResult = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([self.assetCollectionPlaceholder.localIdentifier], options: nil) print(collectionFetchResult) self.collection = collectionFetchResult.firstObject as! PHAssetCollection } }) } //save the video to Photos PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(self.VIDEO_URL_FOR_VIDEO_YOU_MADE!) let assetPlaceholder = assetRequest!.placeholderForCreatedAsset self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.collection, options: nil) let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.collection, assets: self.photosAsset) albumChangeRequest!.addAssets([assetPlaceholder!]) }, completionHandler: { success, error in if success { print("added video to album") }else if error != nil{ print("handle error since couldn't save video") } } })
我改进了@ricardopereira和@ColossalChris代码。 在扩展中添加了video,并在PHAsset之上添加了另一个扩展,以消除编译错误。 快速工作2.1。 示例用法:
#import "Yourtargetname-Swift.h"//important! NSURL *videoURL = [[NSURL alloc] initFileURLWithPath:PATH_TO_VIDEO]; [PHPhotoLibrary saveVideo:videoURL albumName:@"my album" completion:^(PHAsset * asset) { NSLog(@"success"); NSLog(@"asset%lu",(unsigned long)asset.pixelWidth); }];
导入两个swift文件: https : //github.com/kv2/PHPhotoLibrary-PhotoAsset.swift
只要为目标导入swift头文件,就可以在objective-c中使用(请参阅ViewController.m文件)。