How do I save a movie to a custom named album in swift

2.6k views Asked by At

in objective C one could save a video to a custom named photo album using code similar to what follows

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

I can't figure out how to do this ins swift, when I look at the definition of ALAssetsLibrary I don't see any function call toAlum or savedImage. I can't seem to figure this out?

1

There are 1 answers

2
user379468 On

Here is the code I used

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

            })

keep in mind what I'm doing is copying a file from my bundle into this album (pre populateing it with videos)