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

时间:2014-11-10 23:20:13

标签: swift ios8 alassetslibrary

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

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

我无法弄清楚如何快速执行此操作,当我查看ALAssetsLibrary的定义时,我没有看到任何函数调用toAlum或savedImage。我似乎无法弄明白这一点?

1 个答案:

答案 0 :(得分:2)

这是我使用的代码

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

            })

请记住我正在做的是将我的文件夹中的文件复制到此相册中(预先填充视频)