2
votes

I came through the following link which demonstrated how to copy file from Gallery to application or other directory (in right answer):

How to copy an image file from iOS Photo Library (ALAssetsLibrary) to the local directory of an App?

But with ALAssetsLibrary class documentation Apple said its now deprecated as of iOS 9.0 instead use Photos framework.

The Assets Library framework is deprecated as of iOS 9.0. Instead, use the Photos framework instead, which in iOS 8.0 and later provides more features and better performance for working with a user’s photo library. For more information, see Photos Framework Reference.

How I can use Photos framework to copy assets from Gallery to other URL?

3

3 Answers

6
votes

This seems fairly easy. I added an example code for someone whom it may help:

    var item: PHAsset! // you update with actual PHAsset at runtime
    let docuPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.ApplicationDirectory, NSSearchPathDomainMask.UserDomainMask, true) as NSArray
    let targetImgeURL = (docuPath[0] as! String) + "/IMG_0005.JPG"
    let phManager = PHImageManager.defaultManager()
    let options = PHImageRequestOptions()
    options.synchronous = true; // do it if you want things running in background thread
    phManager.requestImageDataForAsset(item, options: options)
    {   imageData,dataUTI,orientation,info in

        if let newData:NSData = imageData
        {
            try! newData.writeToFile(targetImgeURL, atomically: true)
        }
    }
1
votes

Here is the Objective C solution.

  -(NSURL*)createVideoCopyFromReferenceUrl:(NSURL*)inputUrlFromVideoPicker{

        NSURL __block *videoURL;
        PHFetchResult *phAssetFetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[inputUrlFromVideoPicker ] options:nil];
        PHAsset *phAsset = [phAssetFetchResult firstObject];
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_enter(group);

        [[PHImageManager defaultManager] requestAVAssetForVideo:phAsset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {

            if ([asset isKindOfClass:[AVURLAsset class]]) {
                NSURL *url = [(AVURLAsset *)asset URL];
                NSLog(@"Final URL %@",url);
                NSData *videoData = [NSData dataWithContentsOfURL:url];

                // optionally, write the video to the temp directory
                NSString *videoPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%f.mp4",[NSDate timeIntervalSinceReferenceDate]]];

                videoURL = [NSURL fileURLWithPath:videoPath];
                BOOL writeResult = [videoData writeToURL:videoURL atomically:true];

                if(writeResult) {
                    NSLog(@"video success");
                }
                else {
                    NSLog(@"video failure");
                }
                 dispatch_group_leave(group);
                // use URL to get file content
            }
        }];
        dispatch_group_wait(group,  DISPATCH_TIME_FOREVER);
        return videoURL;
    }
0
votes

In Swift use below set of code

import Photos

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    self.dismissViewControllerAnimated(true, completion: nil)

if let referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
    let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([referenceURL], options: nil)
    if let phAsset = fetchResult.firstObject as? PHAsset {
        PHImageManager.defaultManager().requestAVAssetForVideo(phAsset, options: PHVideoRequestOptions(), resultHandler: { (asset, audioMix, info) -> Void in
            if let asset = asset as? AVURLAsset {
                let videoData = NSData(contentsOfURL: asset.URL)

                // optionally, write the video to the temp directory
                let videoPath = NSTemporaryDirectory() + "tmpMovie.MOV"
                let videoURL = NSURL(fileURLWithPath: videoPath)
                let writeResult = videoData?.writeToURL(videoURL, atomically: true)

                if let writeResult = writeResult where writeResult {
                    print("success")
                }
                else {
                    print("failure")
                }
            }
        })
    }
}

}