如何快速将电影保存到自定义命名专辑中

Posted

技术标签:

【中文标题】如何快速将电影保存到自定义命名专辑中【英文标题】:How do I save a movie to a custom named album in swift 【发布时间】:2014-11-10 23:20:13 【问题描述】:

在目标 C 中,可以使用类似于以下代码的代码将视频保存到自定义命名的相册中

-(void)saveImage:(UIImage*)image
         toAlbum:(NSString*)albumName
withCompletionBlock:(SaveImageCompletion)completionBlock;

我无法弄清楚如何快速执行此操作,当我查看 ALAssetsLibrary 的定义时,我没有看到任何对 Alum 或 savedImage 的函数调用。我好像搞不懂?

【问题讨论】:

他们似乎有点变化-> writeImageToSavedPhotosAlbum:metadata:completionBlock: 我看到了,还有 writeVideoSavedPhotosAlbum: 但是如何指定要使用的相册名称? 看来这个 API 在 ios 8 上发生了很大的变化,除了阅读文档之外你别无选择。这来自 Apple:重要在 iOS 8.0 及更高版本中,使用 Photos 框架而不是 Assets Library 框架。 Photos 框架为使用用户的照片库提供了更多功能和更好的性能。见Photos Framework Reference 【参考方案1】:

这是我使用的代码

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)
                        )
                )

            )

记住我正在做的是将文件从我的捆绑包复制到这张专辑中(预先用视频填充它)

【讨论】:

如果没有您的帖子,我永远不会意识到在 PhotoLib 和 iOS 上的集合之间传递资产时必须创建占位符。谢谢! this let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset) 导致访问 nil 值出错

以上是关于如何快速将电影保存到自定义命名专辑中的主要内容,如果未能解决你的问题,请参考以下文章

如何将图像保存到自定义相册?

如何将自定义视图拖动为 Xcode 6 中自定义视图的私有属性

如何将视频文件保存到自定义资源库?

如何将单个 JSON 数据保存到自定义字段?

将照片保存到自定义库

将视频保存到自定义相册 iOS