40

I'm converting our app over to use the Photos Framework of iOS8, the ALAsset framework is clearly a second class citizen under iOS8.

I'm having a problem is that our architecture really wants an NSURL that represents the location of the media on "disk." We use this to upload the media to our servers for further processing.

This was easy with ALAsset:

    ALAssetRepresentation *rep = [asset defaultRepresentation];
    self.originalVideo = rep.url;

But I'm just not seeing this ability in PHAsset. I guess I can call:

    imageManager.requestImageDataForAsset

and then write it out to a temp spot in the file system but that seems awfully heavyweight and wasteful, not to mention potentially slow.

Is there a way to get this or am I going to have refactor more of my app to only use NSURLs for iOS7 and some other method for iOS8?

Paul Cezanne
  • 8,560
  • 7
  • 55
  • 85
  • Looking at my code some more the only reason I use the URL is to feed it to AVAssetExportSession and/or AVAssetImageGenerator so I guess I'll just replace those calls. Then again, once I feed it to those I still need an NSURL to pass to our uploader, so I'll bet the issue is still there. – Paul Cezanne Sep 24 '14 at 21:11
  • 1
    From your comment here it seems you already figured it out for yourself, but for others to see this here: If all you need is to use the URL for older methods (like AVAssetExportSession) you can probably use the newer versions of them that match a PHAsset rather than an AVAsset (AVURLAsset). For example: requestExportSessionForVideo and requestPlayerItemForVideo of PHImageManager.defaultManager() – emem Sep 01 '15 at 11:04

10 Answers10

24

If you use [imageManager requestAVAssetForVideo...], it'll return an AVAsset. That AVAsset is actually an AVURLAsset, so if you cast it, you can access it's -url property.

I'm not sure if you can create a new asset out of this, but it does give you the location.

jlw
  • 3,038
  • 1
  • 16
  • 23
  • you again! :- ) I had forgotten about this question. I went down a different path, see my other question, but this is clearly correct also. BTW, you didn't work for Kodak in the 80s did you? Knew a jlw back then... – Paul Cezanne Oct 01 '14 at 15:01
  • 4
    maybe a dumb question, but does this work for images as well as video? – gdbj Dec 25 '14 at 06:33
  • 1
    This doesn't work for slo-mo videos which will return AVCompostion whose super class is AVAsset. AVAsset has subclasses like AVURLAsset and AVAsset. – rishu1992 Aug 25 '15 at 08:18
  • 9
    @rishu1992 For slo-mo videos, grab the AVComposition's AVCompositionTrack (of mediaType AVMediaTypeVideo), grab its first segment (of type AVCompositionTrackSegment), and then access its sourceURL property. – jlw Aug 25 '15 at 11:52
  • @rishu1992 This doesn't work for images, it returns a nil AVAsset – Xys Jul 12 '17 at 15:47
19

SWIFT 2.0 version This function returns NSURL from PHAsset (both image and video)

func getAssetUrl(mPhasset : PHAsset, completionHandler : ((responseURL : NSURL?) -> Void)){

    if mPhasset.mediaType == .Image {
        let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
        options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
            return true
        }
        mPhasset.requestContentEditingInputWithOptions(options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [NSObject : AnyObject]) -> Void in
            completionHandler(responseURL : contentEditingInput!.fullSizeImageURL)
        })
    } else if mPhasset.mediaType == .Video {
        let options: PHVideoRequestOptions = PHVideoRequestOptions()
        options.version = .Original
        PHImageManager.defaultManager().requestAVAssetForVideo(mPhasset, options: options, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [NSObject : AnyObject]?) -> Void in

            if let urlAsset = asset as? AVURLAsset {
                let localVideoUrl : NSURL = urlAsset.URL
                completionHandler(responseURL : localVideoUrl)
            } else {
                completionHandler(responseURL : nil)
            }
        })
    }

}
Adam Studenic
  • 1,975
  • 2
  • 24
  • 22
  • How am i able to read the content of an Path? It seems like when i am trying to fetch with NSData(contentsOfFile: "...") it's not going to get fetched. Is there a way to read this file from Path like "file:///var/mobile/Media/PhotoData/PhotoCloudSharingData/211607360/28D50A9E-57E3-4280-B75D-7DC228A19147/100CLOUD/IMG_1170.JPG" – Kevin Lieser Mar 12 '16 at 14:20
  • var path: String = pathURL.path() var data: NSData = NSFileManager.defaultManager().contentsAtPath(path) – Adam Studenic Mar 14 '16 at 09:10
  • 7
    It seems like i have no access or something (access to photo library is granted). When i try NSFileManager.defaultManager().fileExistsAtPath() it return false so i am also not able to get the contents of the file. But the file definitely exists. – Kevin Lieser Mar 14 '16 at 12:34
  • 2
    It's crazy that this hasn't been solved yet. I need a reliable url with permanent read access to be used for a `NSURLSessionUploadTask` to work. Since the iPhone's storage is so limited, I cannot copy all the assets to a temp location just for uploading. What would an error message for the user look like: "We are sorry you cannot upload your files to our server because the storage on your device is too low???" Nobody will understand this – fruitcoder Jul 20 '16 at 09:27
  • It worked like a charm for the video part, I just had to adapt it for objective C. Thanks a lot ! – Alexandre Rozier Oct 07 '16 at 11:15
  • 1
    If you are using Objective-C instead of Swift see answer by Sahil. It worked for me for plain Images. – Alyoshak Oct 31 '17 at 16:16
9

If you have a PHAsset, you can get the url for said asset like this:

[asset requestContentEditingInputWithOptions:editOptions
                           completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
    NSURL *imageURL = contentEditingInput.fullSizeImageURL;
}];
Sahil
  • 1,258
  • 12
  • 19
9

Use the new localIdentifier property of PHObject. (PHAsset inherits from this).

It provides similar functionality to an ALAsset URL, namely that you can load assets by calling the method

+[PHAsset fetchAssetsWithLocalIdentifiers:identifiers options:options]
bcattle
  • 10,155
  • 5
  • 54
  • 73
5

All the above solutions won't work for slow-motion videos. A solution that I found handles all video asset types is this:

func createFileURLFromVideoPHAsset(asset: PHAsset, destinationURL: NSURL) {
    PHCachingImageManager().requestAVAssetForVideo(self, options: nil) { avAsset, _, _ in
        let exportSession = AVAssetExportSession(asset: avAsset!, presetName: AVAssetExportPresetHighestQuality)!
        exportSession.outputFileType = AVFileTypeMPEG4
        exportSession.outputURL = destinationURL

        exportSession.exportAsynchronouslyWithCompletionHandler {
            guard exportSession.error == nil else {
                log.error("Error exporting video asset: \(exportSession.error)")
                return
            }

            // It worked! You can find your file at: destinationURL
        }
    }
}
desmond
  • 510
  • 7
  • 8
3

See this answer here.

And this one here.

In my experience you'll need to first export the asset to disk in order to get a fully accessible / reliable URL.

The answers linked to above describe how to do this.

Community
  • 1
  • 1
Alfie Hanssen
  • 16,366
  • 10
  • 64
  • 71
2

Just want to post the hidden gem from a comment from @jlw

@rishu1992 For slo-mo videos, grab the AVComposition's AVCompositionTrack (of mediaType AVMediaTypeVideo), grab its first segment (of type AVCompositionTrackSegment), and then access its sourceURL property. – jlw Aug 25 '15 at 11:52

Oliver Hu
  • 138
  • 7
1

In speking of url from PHAsset, I had once prepared a util func on Swift 2 (although only for playing videos from PHAsset). Sharing it in this answer, might help someone.

static func playVideo (view:UIViewController, asset:PHAsset)

Please check this Answer

Community
  • 1
  • 1
Sazzad Hissain Khan
  • 29,428
  • 20
  • 134
  • 192
1

Here's a handy PHAsset category:

@implementation PHAsset (Utils)

- (NSURL *)fileURL {
    __block NSURL *url = nil;

    switch (self.mediaType) {
        case PHAssetMediaTypeImage: {
            PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
            options.synchronous = YES;
            [PHImageManager.defaultManager requestImageDataForAsset:self
                                                            options:options
                                                      resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
                                                          url = info[@"PHImageFileURLKey"];
                                                      }];
            break;
        }
        case PHAssetMediaTypeVideo: {
            dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
            [PHImageManager.defaultManager requestAVAssetForVideo:self
                                                          options:nil
                                                    resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
                                                        if ([asset isKindOfClass:AVURLAsset.class]) {
                                                            url = [(AVURLAsset *)asset URL];
                                                        }
                                                        dispatch_semaphore_signal(semaphore);
                                                    }];
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            break;
        }
        default:
            break;
    }
    return url;
}

@end
Lizhen Hu
  • 425
  • 6
  • 12
0

I had similiar problem with video files, what worked for me was:

NSString* assetID = [asset.localIdentifier substringToIndex:(asset.localIdentifier.length - 7)];
NSURL* videoURL = [NSURL URLWithString:[NSString stringWithFormat:@"assets-library://asset/asset.mov?id=%@&ext=mov", assetID]];

Where asset is PHAsset.

Gamec
  • 344
  • 1
  • 6